Standard I/O
- The standard I/O functions are implemented “on top of” (using) the Unix I/O functions.
- When a file is opened, a
file descriptor
is returned, and that descriptor is then used for all subsequent I/O operations. - When we open a stream, the standard I/O function
fopen
returns a pointer to aFILE
object which usually contains:file descriptor
: for actual I/O- A pointer to a buffer for the stream
- Buffer size
- …
- An example:
-
A stream of type FILE is an abstraction for a file descriptor and a stream buffer.
-
The purpose of the stream buffer is to minimize the number of expensive Linux I/O system calls.
-
Standard I/O support 2 kinds of files:
text
andbinary
File operations
Opening and closing a stream
- Example:
fopen
opens a specified file
- Example:
freopen
Reading and writing a stream
- Example:
Temporary file
Buffering
- The goal of the buffering provided by the standard I/O library is to use the minimum number of read and write calls.
- Data written to a stream is actually stored in a buffer area in memory; when it’s full (or the stream is closed), the buffer is “flushed” (written to the actual output device).
- Input streams can be buffered in a similar way: the buffer contains data from the input device; input is read from this buffer instead of the device itself.
- Example:
fflush