Is stdout Ever Anything Other and not a console window?

From http://www.cplusplus.com/reference/iostream/cout/ :

By default, most systems have their own standard output installed on the console, where text messages are displayed, although this can usually be redirected.

I have never heard of a system where stdout is something other than a console window, by default or otherwise. I see how redirecting this can be useful on systems where printing is an expensive operation, but that should not be a problem on modern computers, right?

+6
source share
7 answers

On most systems, you can redirect standard input / output / error to other file descriptors or locations.

For example (on Unix):

 ./appname > output 

Redirects stdout from appname to a file called output.

 ./appname 2> errors > output 

Redirect stdout to a file named output and all errors from stderr to the file with errors.

On Unix systems, you can also open the program for the file descriptor and point it to stdin , for example:

 echo "input" > input cat input | ./appname 

This will lead to reading the program from the channel for stdin .


Here's how to unix various utilities together on unix to create one larger tool.

 find . -type f | ./appname | grep -iv "search" 

This will run the find and print its output and pass it to. / appname, then the appname output will be sent to the grep input, which then searches for the word β€œsearch”, displaying only the results that match.

It allows many small utilities to have a very powerful effect.


Think of > , < and | like plumbing.

> looks like a sink leak, it receives data and stores it where you want to place it. When the shell encounters > , it will open the file.

 > file 

When the shell sees above, there will be an open file using a standard system call, and remember this file descriptor. In the above case, since there is no input, it will create an empty file and allow you to enter more commands.

 banner Hello 

This command writes Hello in truly capital letters to the console and causes it to scroll (I use Unix here, as this is what I know best). The output is simply written to the standard version. Using the "receiver" ( > ), we can control where the output goes, therefore

 banner Hello > bannerout 

will redirect all the data from the standard banner output to the file descriptor that the shell opened and thus will be written to a file called bannerout .

Pipes work similarly > in that they help control the flow where the data goes. Pipes, however, cannot write to files and can only be used to transfer data from one point to another.

For example, here water flows through several substations and waste treatment:

 pump --from lake | treatment --cleanse-water | pump | reservoir | pump > glass 

Water flows from the lake, through a pipe to the water treatment plant, from the plant back to the pump, which moves it to the tank, then it is pumped again into the municipal water pipes and through your sink into your glass.

Note that the pipes simply connect all the outlets together, eventually they end in your glass.

The same thing happens with commands and their processing in a shell on Linux. He also follows the path to achieving the end result.

Now there is one last thing that I have not talked about in previous instructions, that is, the input symbol < . What it does is read from a file and displays it on stdin for programs.

 cat < bannerout 

It will simply print what has been stored in the bandage. This can be used if you have a file that you want to process, but do not want to add cat <file> due to the unwillingness to run an additional command in the chain.

So try the following:

 echo "Hello" > bannerinput banner < bannerinput 

First, the string "Hello" will be placed in the bannerinput file, and then when your launch banner is read from the bannerinput file.

Hope this helps you understand how redirection and copying work on Unix (some, if not most, will apply to Windows as well).

+8
source

Of course it could be. Perhaps I want to redirect the standard to a text file, another process, socket, whatever.

By default, this is the console, but there are many reasons for redirecting it that are most useful (in stages with the Unix philosophy) - redirecting the output of one program to the input of another program. This allows you to create many small, lightweight programs that feed on one another and work as separate parts of a larger system.

In principle, this is a simple but powerful data exchange mechanism. This is more popular on * nix systems for the reason I mentioned above, but this also applies to Windows.

+14
source

So far, all the answers have been in the context of the thing (shell, whatever) that calls the program. The program itself can do stdout something other than a terminal. The C standard library provides freopen , which allows the programmer to redirect stdout to any compatible environment. POSIX provides a number of other mechanisms ( popen , fdopen , ...), which gives the programmer even more control. I suspect that Windows provides similar mechanisms.

+4
source

Any number of things can happen with three standard file descriptors 0, 1, and 2. Anyone can start a new process with file descriptors attached to whatever they like.

For example, the GNU screen places output on a pipe and allows you to dynamically reattach a session. SSH prints the result and returns it to the other end. And, of course, all the many shell editors regularly use file descriptor manipulations.

+3
source

In order for the program to have stdout , it must be launched in a hosted implementation (one with the operating system) or in a standalone implementation with additional functions.

It's hard for me to define such an implementation without any console, but suppose for a moment that Mars Rover has a full OS and is programmed in C (or C ++) and does not have this console

 /* 2001-07-15: JPL: stdout is the headquarters */ puts("Help. I'm stuck."); 

could send a message to NASA headquarters.

+3
source

Both Windows and Linux will redirect stdout to a file if you run the program as follows:

my_program > some_file

This is the most common case, but many other types of redirection are possible. On Linux, you can redirect stdout to anything that supports a file descriptor interface, such as a pipe, socket, file, and other other things.

+2
source

One simple example of a case where you need to redirect stdout is the transfer of information to another program. The Unix / Linux ps command creates a list of processes owned by the current user. If this list was long and you wanted to search for a specific process, you can enter

 ps | grep thing 

which redirects stdout of ps to the stdin of grep thing .

+1
source

Source: https://habr.com/ru/post/891167/


All Articles