Previous Page
Next Page

13.1. Streams

From the point of view of a C program, all kinds of files and devices for input and output are uniformly represented as logical data streams , regardless of whether the program reads or writes a character or byte at a time, or text lines, or data blocks of a given size. Streams in C can be either text or binary streams , although on some systems even this difference is nil. Opening a file by means of the function fopen( ) (or tmpfile( )) creates a new stream, which then exists until closed by the fclose( ) function. C leaves file management up to the execution environmentin other words, the system on which the program runs. Thus a stream is a channel by which data can flow from the execution environment to the program, or from the program to its environment. Devices, such as consoles, are addressed in the same way as files.

13.1.1. Text Streams

A text stream transports the characters of a text that is divided into lines. A line of text consists of a sequence of characters ending in a newline character. A line of text can also be empty, meaning that it consists of a newline character only. The last line transported may or may not have to end with a newline character, depending on the implementation.

The internal representation of text in a C program is the same regardless of the system on which the program is running. Thus text input and output on a given system may involve removing, adding, or altering certain characters. For example, on systems that are not Unix-based, end-of-line indicators ordinarily have to be converted into newline characters when reading text files , as on Windows systems for instance, where the end-of-line indicator is a sequence of two control characters, \r (carriage return) and \n (newline). Similarly, the control character ^Z (character code 26) in a text stream on Windows indicates the end of the stream.

As the programmer, you generally do not have to worry about the necessary adaptations, because they are performed automatically by the I/O functions in the standard library. However, if you want to be sure that an input function call yields exactly the same text that was written by a previous output function call, your text should contain only the newline and horizontal tab control characters, in addition to printable characters. Furthermore, the last line should end with a newline character, and no line should end with a space immediately before the newline character.

13.1.2. Binary Streams

A binary stream is a sequence of bytes that are transmitted without modification. In other words, the I/O functions do not involve any interpretation of control characters when operating on binary streams . Data written to a file through a binary stream can always be read back unchanged on the same system. However, in certain implementations there may be additional zero-valued bytes appended at the end of the stream.

Binary streams are normally used to write binary datafor example, database recordswithout converting it to text. If a program reads the contents of a text file through a binary stream, then the text appears in the program in its stored form, with all the control characters used on the given system.

On common Unix systems, there is no difference between text streams and binary streams.

Previous Page
Next Page