The standard I/O library provides a simple and efficient buffered stream I/O interface. Input and output is mapped into logical data streams and the physical I/O characteristics are concealed.
A stream is associated with an external file (which may be a physical device) by
opening a file, which may involve creating a new file. Creating an existing file causes its former contents to be discarded. If a file can support positioning requests (such as a disk file, as opposed to a terminal) then a
file position indicator associated with the stream is positioned at the start of the file (byte zero), unless the file is opened with append mode. If append mode is used, the position indicator will be placed the end-of-file. The position indicator is maintained by subsequent reads, writes and positioning requests. All input occurs as if the characters were read by successive calls to the
fgetc(3) function; all output takes place as if all characters were read by successive calls to the
fputc(3) function.
A file is disassociated from a stream by
closing the file. Output streams are flushed (any unwritten buffer contents are transferred to the host environment) before the stream is disassociated from the file. The value of a pointer to a
FILE object is indeterminate after a file is closed (garbage).
A file may be subsequently reopened, by the same or another program execution, and its contents reclaimed or modified (if it can be repositioned at the start). If the main function returns to its original caller, or the
exit(3) function is called, all open files are closed (hence all output streams are flushed) before program termination. Other methods of program termination, such as
abort(3) do not bother about closing files properly.
This implementation needs and makes no distinction between “text” and “binary” streams. In effect, all streams are binary. No translation is performed and no extra padding appears on any stream.
At program startup, three streams are predefined and need not be opened explicitly:
1.
standard input for reading conventional input,
2.
standard output for writing conventional output, and
3.
standard error for writing diagnostic output.
These streams are abbreviated
stdin,
stdout, and
stderr.
Initially, the standard error stream is unbuffered; the standard input and output streams are fully buffered if and only if the streams do not refer to an interactive or “terminal” device, as determined by the
isatty(3) function. In fact,
all freshly-opened streams that refer to terminal devices default to line buffering, and pending output to such streams is written automatically whenever an such an input stream is read. Note that this applies only to “true reads”; if the read request can be satisfied by existing buffered data, no automatic flush will occur. In these cases, or when a large amount of computation is done after printing part of a line on an output terminal, it is necessary to
fflush(3) the standard output before going off and computing so that the output will appear. Alternatively, these defaults may be modified via the
setvbuf(3) function.