Jump to: navigation, search

Description

A stream is a logical entity that represents a file or device that can accept input or output. All input and output functions in standard C operate on data streams. Streams can be divided into text streams and binary streams. Streams may also have an orientation towards byte I/O or wide I/O.

Text streams

A stream that is opened in text mode will read and write files in the implementation's native text file format. The data read from or written to a text stream is in the form of lines of text, each terminated by a single newline character ('\n'). The implementation will take care of conversion from this format into the native format.

On Unix systems there is no conversion necessary and none is performed. On Windows or DOS systems, "\r\n" (CRLF) is translated to "\n" (LF) and vice-versa. On Macintosh systems, "\r" (CR) is translated to "\n" (LF) and vice-versa. Some older operating systems have record-based text file formats that require more extensive conversion.

Binary streams

A stream that is opened in binary mode can read and write raw data, disregarding any native file format issues. There is no conversion or translation performed.

Predefined streams

Three streams are defined by the implementation and automatically opened when your C program starts up. They are the standard input, standard output and standard error streams, which may be accessed through the identifiers stdin, stdout and stderr respectively.

Typically, user input typed into the program can be read through the standard input stream, while program output written to the standard output and standard error streams will appear on the user's screen. The standard output stream is designed for normal program output, while standard error is designed for diagnostics and error messages.

Most operating systems allow the user to redirect the input or output of these streams to other files. Some implementations run on hardware that lacks keyboards or screens; in that case the predefined streams may be mapped to a serial line, printer or other file.

References

The C Standard, 7.19.3 (C99 numbering)

Personal tools