The iostream library is an object-oriented library that provides input and output functionality using streams.
A stream is an abstraction that represents a device on which input and ouput operations are performed. A stream can basically be represented as a source or destination of characters of indefinite length.
Streams are generally associated to a physical source or destination of characters, like a disk file, the keyboard, or the console, so the characters gotten or written to/from our abstraction called stream are physically input/output to the physical device. For example, file streams are C++ objects to manipulate and interact with files; Once a file stream is used to open a file, any input or output operation performed on that stream is physically reflected in the file.
To operate with streams, C++ provides the standard iostream
library, which contains the following elements:
- Basic class templates
- The base of the iostream library is the hierarchy of class templates. The class templates provide most of the functionality of the library in a type-independent fashion.
This is a set of class templates, each one having two template parameters: the char type (charT) parameter, that determines the type of elements that are going to be manipulated and the traits parameter, that provides additional characteristics specific for a particular type of elements.
The class templates in this class hierarchy have the same name as their char-type instantiations but with the prefix basic_. For example, the class template which istream is instantiated from is called basic_istream, the one from which fstream is is called basic_fstream, and so on... The only exception is ios_base, which is by itself type-independent, and therefore is not based on a template, but is a regular class.
- Class template instantiations
- The library incorporates two standard sets of instantiations of the entire iostream class template hierarchy: one is narrow-oriented, to manipulate elements of type char and another one, wide-oriented, to manipulate elements of type wchar_t.
The narrow-oriented (char type) instantiation is probably the better known part of the iostream library. Classes like ios, istream and ofstream are narrow-oriented. The diagram on top of this page shows the names and relationships of narrow-oriented classes.
The classes of the wide-oriented (wchar_t) instatiation follow the same naming conventions as the narrow-oriented instantiation but with the name of each class and object prefixed with a w character, forming wios, wistream and wofstream, as an example.
- Standard objects
- As part of the iostream library, the header file <iostream> declares certain objects that are used to perform input and output operations on the standard input and output.
They are divided in two sets: narrow-oriented objects, which are the popular cin, cout, cerr and clog and their wide-oriented counterparts, declared as wcin, wcout, wcerr and wclog.
- The iostream classes barely use fundamental types on their member's prototypes. They generally use defined types that depend on the traits used in their instantiation. For the default char and wchar_t instantiations, types streampos, streamoff and streamsize are used to represent positions, offsets and sizes, respectively.
- Manipulators are global functions designed to be used together with insertion (<<) and extraction (>>) operators performed on iostream stream objects. They generally modify properties and formatting settings of the streams. endl, hex and scientific are some examples of manipulators.
The library and its hierarchy of classes is split in different files:
- <ios>, <istream>, <ostream>, <streambuf> and <iosfwd> aren't usually included directly in most C++ programs. They describe the base classes of the hierarchy and are automatically included by other header files of the library that contain derived classes.
- <iostream> declares the objects used to communicate through the standard input and output (including cin and cout).
- <fstream> defines the file stream classes (like the template basic_ifstream or the class ofstream) as well as the internal buffer objects used with these (basic_filebuf). These classes are used to manipulate files using streams.
- <sstream>: The classes defined in this file are used to manipulate string objects as if they were streams.
- <iomanip> declares some standard manipulators with parameters to be used with extraction and insertion operators to modify internal flags and formatting options.
The names, prototypes and examples included in this reference for the iostream classes mostly describe and use the char
instantiations of the class templates instead of the templates themselves, even though these classes are only one of their possible instantiations. We believe this provides a better readability and is arguably as easy to obtain the names and prototypes of the basic template from the char
instantiation as the opposite.
Elements of the iostream library (char instantitation)
- Base class for streams (class)
- Base class for streams (type-dependent components) (class)
- Input stream (class)
- Output Stream (class)
- Input/output stream (class)
- Input file stream class (class)
- Output file stream (class)
- Input/output file stream class (class)
- Input string stream (class)
- Output string stream (class)
- Input/output string stream (class)
- Base buffer class for streams (class)
- File stream buffer (class)
- String stream buffer (class)
- Standard input stream (object)
- Standard output stream (object)
- Standard output stream for errors (object)
- Standard output stream for logging (object)
- Stream position class template (class template)
- Stream offset type (type)
- Stream position type (type)
- Stream size type (type)
- Alphanumerical bool values (function)
- Use decimal base (function)
- Insert newline and flush (function)
- Insert null character (function)
- Use fixed floating-point notation (function)
- Flush stream buffer (function)
- Use hexadecimal base (function)
- Adjust field by inserting characters at an internal position (function)
- Adjust output to the left (function)
- No alphanumerical bool values (function)
- Do not show numerical base prefixes (function)
- Do not show decimal point (function)
- Do not show positive signs (function)
- Do not skip whitespaces (function)
- Do not force flushes after insertions (function)
- Do not generate upper case letters (function)
- Use octal base (function)
- Reset format flags (function)
- Adjust output to the right (function)
- Use scientific floating-point notation (function)
- Set basefield flag (function)
- Set fill character (function)
- Set format flags (function)
- Set decimal precision (function)
- Set field width (function)
- Show numerical base prefixes (function)
- Show decimal point (function)
- Show positive signs (function)
- Skip whitespaces (function)
- Flush buffer after insertions (function)
- Generate upper-case letters (function)
- Extract whitespaces (function)