The stream classes

Stream IO

1.1  Streams

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 aninput 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.
IOstreams.png
C++ provides both the formatted and unformatted IO functions. In formatted or high-level IO, bytes are grouped and converted to types such as intdouble, 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:
  1. Construct a stream object.
  2. Connect (Associate) the stream object to an actual IO device (e.g., keyboard, console, file, network, another program).
  3. 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).
  4. Disconnect (Dissociate) the stream to the actual IO device (e.g., close the file).
  5. Free the stream object.

1.2  C++ IO Headers, Templates and Classes

Headers
C++ IO is provided in headers <iostream> (which included <ios><istream><ostream> and <streambuf>), <fstream> (for file IO), and <sstream> (for string IO). Furthermore, the header<iomanip> provided manipulators such as setw()setprecision()setfill() and setbase() for formatting.
IOclasses.png