C/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs (just like water and oil flowing through a pipe). In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program. In output operations, data bytes flow from the program to an output sink (such as console, file, network or another program). Streams acts as an intermediaries between the programs and the actual IO devices, in such the way that frees the programmers from handling the actual devices, so as to archive device independent IO operations.
C++ provides both the formatted and unformatted IO functions. In formatted or high-level IO, bytes are grouped and converted to types such as
int
, double
, string or user-defined types. In unformatted or low-level IO, bytes are treated as raw bytes and unconverted. Formatted IO operations are supported via overloading the stream insertion (<<
) and stream extraction (>>
) operators, which presents a consistent public IO interface.
To perform input and output, a C++ program:
- Construct a stream object.
- Connect (Associate) the stream object to an actual IO device (e.g., keyboard, console, file, network, another program).
- Perform input/output operations on the stream, via the functions defined in the stream's pubic interface in a device independent manner. Some functions convert the data between the external format and internal format (formatted IO); while other does not (unformatted or binary IO).
- Disconnect (Dissociate) the stream to the actual IO device (e.g., close the file).
- Free the stream object.
No comments:
Post a Comment