Standard data streams

from Wikipedia, the free encyclopedia
The standard data streams for input, output, and error

The standard data streams (English: standard streams ) are three data streams for input and output in the Unix operating system or in related operating systems . They are also supported by the C standard library . Many programs automatically use standard input or standard output if no files are specified for input or output on the command line .

Standard input (stdin)

Data can be read into a program via the standard input. Usually it is connected to the keyboard; H. Programs receive user input through standard input. Under Unix, the standard input corresponds to the device file / dev / stdin , the file descriptor has the number 0.

Example: Here the standard input for the less program is read from the file eingaben.txt .

$ less < eingaben.txt

Standard output (stdout)

A program can output data via the standard output. Usually it is connected to the monitor; H. Programs send output text to the user via standard output. Under Unix, the standard output corresponds to the device file / dev / stdout , the file descriptor has the number 1.

Example: Here, the standard output of the find program is redirected to the output.txt file .

$ find . -name '*.html' > ausgaben.txt

synonymous:

$ find . -name '*.html' 1> ausgaben.txt

Standard error output (stderr)

The standard error output is a second output data stream, which is intended to output error and status messages. Usually it is also connected to the monitor; however, it can be redirected separately from the standard output, so that error messages are not mixed up with the user data output. Under Unix, the standard error output corresponds to the device file / dev / stderr , the file descriptor has the number 2.

Example 1: Here the error messages from the find program are redirected to the file error messages.txt , while the standard output is forwarded to the less program via a pipe .

$ find . -name '*.html' 2> fehlermeldungen.txt | less

Output redirection

Example 2: Redirecting the error messages in stdout :

$ find . -name '*.html' 2>&1   | less

Here this is to be understood as follows: "Redirect stderr (also) to where stdout is now pointing."

Example 3:

$ find . -name '*.html' 2>&1 1> gefundene.txt | less

stderr is redirected to the screen / Scrn:, then stdout to the file ' found.txt '; stderr is not redirected into the file because it was not directed "to stdout", but rather "to where stdout points (e)". Chaining with less causes the error messages to be displayed page by page.

Example 4:

$ find . -name '*.html' 1> gefundene.txt 2>&1 | less

stdout is redirected to the file found.txt, then stderr to where stdout is currently pointing (also to the file), nothing remains for less !

Programming languages ​​C and C ++

In the C programming language , three file pointers of the type FILE * with the names stdin , stdout and stderr are defined in the header file stdio.h . These virtual files are usually opened automatically when the program is started and can be used with most commands that access files.

The IOStream Library, which is part of the standard library, is used under C ++ . It is integrated with the header file iostream (without file extension); the standard data streams are finally accessed via the stream objects std :: cout , std :: cin and std :: cerr .

Since this behavior is standardized by C ++ or C, these standard data streams can also be found in non-Unix operating systems (such as Windows ) or are simulated accordingly.

Java programming language

In the Java programming language , three streams are generated in the java.lang.System class. System.in as InputStream, System.out as PrintStream and System.err also as PrintStream. These streams are automatically opened when the program is started and can be used to access the standard data streams. Since this behavior is standardized by Java, these standard data streams can also be found in non-Unix operating systems or are simulated accordingly.

Python programming language

The standard data streams can also be accessed under Python using the file objects stdin, stdout and stderr provided in the sys module.

See also