Name
Stdin, stdout, stderr-standard I/O Stream
Synopsis Overview
# Include <stdio. h>
Extern file * stdin;
Extern file * stdout;
Extern file * stderr;
Description
Generally, every Unix
Program Three streams are opened at startup, one for input, one for output, and the other for printing diagnostic or error messages. Typically, they are connected to the user's terminal.
(See tty (4 ))
However, it may also point to a file or another device, depending on what the parent process chooses to set (see SH (1)
.)
The input stream is called ''standard input'; the output stream is called ''standard output '';
The error stream is called ''Standard
Error ''. These terms are usually abbreviated as symbols used to reference these files. They are stdin, stdout,
And stderr.
Each of these symbols is a macro in stdio (3) and the type is
Can be used in functions like fprintf (3) or fread (3.
Because file is a buffer package for UNIX file descriptors, you can also use-
Start with the Unix File interface. That is, functions similar to read (2) and lseek (2. And stream
Stdin, stdout, and stderr are associated with file descriptors in integer form, which are 0, 1 and
2. Preprocessor symbols stdin_fileno, stdout_fileno, and stderr_fileno
They are defined in <unistd. h>.
Note: Use both file and-
File descriptors may bring unexpected results, which should be avoided in general. (Posix.1
Section 8.2.3 of the Specification describes in detail how such a mix can be used without errors .)
A simple rule is that file descriptors are controlled by the kernel, while stdio
It is just a library. It means, for example, when you call Exec
Then, the sub-process can inherit all open file descriptors, but the sub-process is slightly delayed.
Because the symbols stdin, stdout, and stderr
If they are specified as macros and assigned values, they cannot be transplanted. Use the library function freopen (3)
The standard stream can be used to point to different files. This function is specifically used for stdin, stdout, and
Stderr is assigned a value again. The standard stream is disabled when exit (3) is called and the program is aborted normally.
Sh (1), CSH (1), open (2), fopen (3), stdio (3)
Considerations
The error stream stderr is not buffered. Output stream stdout
It is a row buffer, if it points to a terminal. Incomplete rows are only allowed when fflush (3) or exit (3) is called)
Or a new line character is printed. This may lead to unexpected results, especially when debugging the output. Standard stream
The buffer mode of (or any other stream) can be set using the setbuf (3) or setvbuf (3) function)
To switch. Note: When stdin is associated with a terminal, the input buffer may exist in the terminal driver.
The stdio buffer is completely unrelated. (Indeed, the average terminal input is in the kernel as a line buffer .)
The kernel can modify the input control by calling tcsetattr (3). For more information, see stty (1), and
Termios (3 ).
Macros stdin, stdout, and stderr follow ANSI X3.159-1989 (''ansi C '')
Standard, which also specifies that the three streams should be opened when the program starts.