The Standard C++ I/O system is constructed from a rather complex system of template classes. These classes are shown here:
Class |
Purpose |
---|---|
basic_ios |
Provides general-purpose I/O operations |
basic_streambuf |
Low-level support for I/O |
basic_istream |
Support for input operations |
basic_ostream |
Support for output operations |
basic_iostream |
Support for input/output operations |
basic_filebuf |
Low-level support for file I/O |
basic_ifstream |
Support for file input |
basic_ofstream |
Support for file output |
basic_fstream |
Support for file input/output |
basic_stringbuf |
Low-level support for string-based I/O |
basic_istringstream |
Support for string-based input |
basic_ostringstream |
Support for string-based output |
basic_stringstream |
Support for string-based input/output |
Also part of the I/O class hierarchy is the nontemplate class ios_base. It provides definitions for various elements of the I/O system.
The C++ I/O system is built upon two related, but different, template class hierarchies. The first is derived from the low-level I/O class called basic_streambuf. This class supplies the basic, low-level input and output operations, and provides the underlying support for the entire C++ I/O system. The classes basic_filebuf and basic_stringbuf are derived from basic_streambuf. Unless you are doing advanced I/O programming, you will not need to use basic_streambuf or its subclasses directly.
The class hierarchy that you will most commonly be working with is derived from basic_ios. This is a high-level I/O class that provides formatting, error checking, and status information related to stream I/O. basic_ios is used as a base for several derived classes, including basic_istream, basic_ostream, and basic_iostream. These classes are used to create streams capable of input, output, and input/output, respectively. Specifically, from basic_istream are derived the classes basic_ifstream and basic_istringstream, from basic_ostream are derived basic_ofstream and basic_ostringstream, and from basic_iostream are derived basic_fstream and basic_stringstream. A base class for basic_ios is ios_base. Thus, any class derived from basic_ios has access to the members of ios_base.
The I/O classes are parameterized for the type of characters that they act upon and for the traits associated with those characters. For example, here is the template specification for basic_ios:
template <class CharType, class Attr = char_traits<CharType> > class basic_ios: public ios_base
Here, CharType specifies the type of character (such as char or wchar_t) and Attr specifies a type that describes its attributes. The generic type char_traits is a utility class that defines the attributes associated with a character.
The I/O library creates two specializations of the template class hierarchies just described: one for 8-bit characters and one for wide characters. Here is a complete list of the mapping of template class names to their character and wide-character versions:
Template Class |
Character-Based Class |
Wide-Character-Based Class |
---|---|---|
basic_ios |
ios |
wios |
basic_istream |
istream |
wistream |
basic_ostream |
ostream |
wostream |
basic_iostream |
iostream |
wiostream |
basic_ifstream |
ifstream |
wifstream |
basic_ofstream |
ofstream |
wofstream |
basic_fstream |
fstream |
wfstream |
basic_istringstream |
istringstream |
wistringstream |
basic_ostringstream |
ostringstream |
wostringstream |
basic_stringstream |
stringstream |
wstringstream |
basic_streambuf |
streambuf |
wstreambuf |
basic_filebuf |
filebuf |
wfilebuf |
basic_stringbuf |
stringbuf |
wstringbuf |
Since the vast majority of programmers will be using character-based I/O, those are the names used by this chapter. Thus, when referring to the I/O classes, we will simply use their character-based names rather then their internal, template names. For instance, this chapter will use the name ios rather than basic_ios, istream rather than basic_istream, and fstream rather than basic_fstream. Remember, parallel classes exist for wide-character streams and they work in the same way as those described here.