C++ Stream Classes for File Handling

- March 04, 2018
A logical interface between data file or input/output device and program is called stream. It is a general name given to a flow of data (a sequence of bytes). The stream may be input stream or output stream.

C++ provides predefined stream classes that are used to handle input/output streams. These are defined in related header files. The related header files are included in the program to use their stream classes. For example, the header file ‘iostream .h’ is commonly used in the C++ program. This header file contains the definitions for stream objects ‘cin’ ‘cout’, ‘cerr’ and ‘clog’ etc. These objects of stream classes are used to exchange data across different parts of computer such as input/output.

Hierarchy of Stream Classes

The stream classes are arranged in the form of hierarchy (like a tree structure). The root (or base) of all the streams is "ios" (input/output stream). This hierarchy is as follows:
 C++ streams structure hierarchy

In C++, the most popular and commonly used stream classes are:
ios It is a main class for all the stream classes that are linked with 'ios' class directly or indirectly. Moreover, this class contains majority of the features that are used to operate C++ streams. (i.e file operation modes, formating flags etc).
istream It is derived through single inheritance from the 'ios' base class that is used to perform input stream operations. The stream extraction operator (>>) is a member of this stream that is used with "cin" object and user defined input stream objects for input operations.
ostream It is opposite to "istream" that is also derived through single inheritance from the 'ios' base class. It is used to perform output stream operations. The stream insertion operator (<<) is a member of this stream that is used with "cout" object and user defined input stream objects for output operations.
iostream It is derived through multiple inheritance from both the "istream" and "ostream" class. This class is used for both input and output operations.

Classes For File Operations

There are some special classes that are used for file handling in C++. These are:

  • ifstream
  • ofstream
  • fstream

Objects of these classes can be associated with data files and we can use their member functions to read and write data to and from data files.
ifstream It's stand for "input file stream" that is used to perform input operations on data files. This class is derived from 'istream' class. The objects of this stream class are only used to read data from a data file.
ofstreamIt's stand for "output file stream" that is used to perform output operations on data files. This class is derived from 'ostream' class. The objects of this stream class are only used to write data into a data file.
fstreamIt is used to perform both input and output operations on data files on the disk. It is derived from 'iostream' class. The objects of this stream class are used to read & write data from and into data file.
For file processing operations, the "fstream.h" header file contains definitions of these classes.
In the same way, "iostream.h" provides the basic service for all standard stream input/output operations. Both "iostream" and "fstream" header files are interrelated to each other.
We must have to include these header files in C++ program for data file operations.