Chapter 13. Input and Output

Programs must be able to write data to files or to physical output devices such as displays or printers, and to read in data from files or input devices such as a keyboard. The C standard library provides numerous functions for these purposes. This chapter presents a survey of the part of the standard library that is devoted to input and output, often referred to as the I/O library. Further details on the individual functions can be found in Part II. Apart from these library functions, the C language itself contains no input or output support at all.

All of the basic functions, macros, and types for input and output are declared in the header file stdio.h. The corresponding declarations for wide character input and output functions—that is, for input and output of characters with the type wchar_t—are contained in the header file wchar.h.

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 environment—in other words, the system on which the program ...

Get C in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.