2.7 Input and Output

The input and output (I/O) routines allow you to read and write data to and from files and devices. In C, there are no predefined file structures; all data items are treated as sequences of bytes. The following three types of I/O functions are available:

Stream

Low-level

Console and port

The stream I/O functions treat data as a stream of individual characters. By choosing among the many stream functions available, you can process data in different sizes and formats, from single characters to large data structures. Stream I/O also provides buffering, which can significantly improve performance.

The low-level I/O routines do not perform buffering and formatting. Instead, they invoke the operating system's input and output capabilities directly. These routines let you access files and peripheral devices at a more basic level than the stream functions.

The console and port I/O routines allow you to read or write directly to a console (keyboard and screen) or an I/O port (such as a printer port). The port I/O routines simply read and write data in bytes. With console I/O routines, some additional options are available, such as detecting whether a character has been typed at the console. You can also choose between echoing characters to the screen as they are read or reading characters without echoing.

The run-time library also provides a number of direct DOS I/O system-call routines. These are described in “System Calls”.

You can perform file I/O operations in two modes: text and binary. The following section describes these modes and their use. You can also ensure that the fflush and _flushall routines write data to storage media rather than to just the operating system's buffers. See “Stream Routines”.

Warning!:

Because stream routines are buffered and low-level routines are not, the two types of routines are generally incompatible. You should use either stream or low-level routines consistently for processing a given file.