A stream is a logical abstraction that isolates input and output operations from the physical characteristics of terminals and structured storage devices.
Streams map a program's data and the data that is actually stored on the external devices. Two forms of mapping are supported, for text streams and for binary streams. Streams also provide buffering, which is a memory management technique that reads and writes large blocks of data. Without buffering, data on an I/O device must be accessed one item at a time. This inefficient I/O processing slows program execution considerably.
The stdio.h functions use buffers in primary storage to intercept and collect data as it is written to or read from a file. When a buffer is full its contents are actually written to or read from the file, thereby reducing the number of I/O accesses. A buffer's contents can be sent to the file prematurely by using the fflush() function.
The stdio.h header offers three buffering schemes: unbuffered, block buffered, and line buffered. The setvbuf() function is used to change the buffering scheme of any output stream. When an output stream is unbuffered, data sent to it are immediately read from or written to the file. When an output stream is block buffered, data are accumulated in a buffer in primary storage. When full, the buffer's contents are sent to the destination file, the buffer is cleared, and the process is repeated until the stream is closed. Output streams are block buffered by default if the output refers to a file. A line buffered output stream operates similarly to a block buffered output stream. Data are collected in the buffer, but are sent to the file when the line is completed with a newline character ( '\n').
A stream is declared using a pointer to a FILE. There are three FILE pointers that are automatically opened for a program: FILE *stdin, FILE *stdout, and FILE *stderr. The FILE pointers stdin and stdout are the standard input and output files, respectively, for interactive console I/O. The stderr file pointer is the standard error output file, where error messages are written to. The stderr stream is written to the console. The stdin and stdout streams are line buffered while the stderr stream is unbuffered.
If a standard input/output stream is closed it is not possible to reopen and reconnect that stream to the console. However, it is possible to reopen and connect the stream to a named file.
The C and C++ input/output facilities share the same stdin, stdout, and stderr streams.