To perform file processing in C++, headers <iostream>
and <fstream>
must be included. Header <fstream>
includes the definitions for the stream class templates basic_ifstream
(for file input), basic_ofstream
(for file output) and basic_fstream
(for file input and output). Each class template has a predefined template specialization that enables char
I/O. In addition, the <fstream>
library provides typedef
aliases for these template specializations. For example, the typedef ifstream
represents a specialization of basic_ifstream
that enables char
input from a file. Similarly, typedef ofstream
represents a specialization of basic_ofstream
that enables char
output to files. Also, typedef fstream
represents a specialization of basic_fstream
that enables char
input from, and output to, files.
These templates derive from the class templates basic_istream
, basic_ostream
and basic_iostream
, respectively. Thus, all member functions, operators and manipulators that belong to these templates (which we described in Chapter 13) also can be applied to file streams. Figure 14.2 summarizes the inheritance relationships of the I/O classes that we’ve discussed to this point.