What Is a Stream?

If you are a C programmer, you know that the C language has no built-in input/output capability. You must use C run-time library functions such as printf, _open, _read, fwrite, and getchar for disk, display, and keyboard I/O. Variations such as sprintf support in-memory formatting.

Like C, C++ does not have built-in I/O capability. All C++ compilers, however, come bundled with a systematic, object-oriented I/O package, known as the “iostream classes.” These stream classes are the most standard of all C++ classes because they were developed by the authors of the C++ language.

The “stream” is the central concept in the iostream classes. You can think of a stream object as a kind of “smart file” that acts as a source and/or destination for bytes. A “stream” is a C++ object with characteristics determined by its specific class and by your customized insertion and extraction operators.

The disk operating system, through its device drivers, extends the concept of a file to include the keyboard, display, printer, and communication ports. The iostream classes, like the C run-time library I/O routines, interact with these extended files. The Microsoft iostream Class Library, however, goes further. Built-in classes support reading and writing to and from memory with syntax identical to the syntax for disk I/O. As you gain familiarity with the library, you can derive your own stream classes to support, for example, interprocess communication or a graphical user interface.