In C++, file input/output services are implemented through a component header file available C++ standard library. This header file is fstream.h .

In C++, a file, at its lowest level, is interpreted simply as a sequence, or stream, of bytes. One aspect of the file I/O library manages the transfer of these bytes. At this level, the concept of a data type is absent. On the other hand, file, at the user level, consists of a sequence of possibly intermixed data types – characters, arithmetic values, class objects etc. A second aspect of file I/O library manages the interface between these two levels.

The fstream.h Header File

The C++ input/output operations are very much similar to the console input and output operations. The file operations also make use of streams as an interface between the programs and the files.

A stream is a general name given to a flow of data. Different streams are used to represent different kinds of data flow. Each stream is associated with a particular class, which contains member functions and definitions for dealing with that particular kind of data flow. For example, the ifstream class represents input disk files.

The stream that supplies data to the program is known as input stream. It reads data from a file and hands it over to the program. The stream that receives data from the program is known as the output stream. It writes the received data to the file.

In C++, to open a file, you must first obtain a stream. There are three types of streams: input, output, and input/output. To create an input stream, you must declare the stream to be of class ifstream. To create an output stream, you must declare it as class ofstream. Streams that would perform both input and output operations must be declared as class fstream. Once a stream has been created, next step is to associate a file with it. Thereafter the file is available (opened) for processing.

Opening of a file can be achieved in two ways:

  • Using the constructor function of the stream class.
  • Using the function called open( ).

The first method is preferred when a single file is used with a stream; however, for managing multiple files with the same stream, the second method is preferred.

Opening a File using Constructor

A constructor of a class initializes an object of its class when it (the object) is being created. Same way, a constructor of stream class (ifstream, ofstream, or fstream) is used to initialize a file stream object with the filename/s passed to them. To open a d ata file, as an input file, we shall create a file stream object of input type i.e., ifstream type.

The above given statement creates an object (i.e. input-file) of input file stream. The object name is a user defined name (i.e., any valid identifier). After creating the ifstream object inpt-file, the file DataFile is opened and attached to the input stream object inpt-file. The data being read from DataFile is been channelized through the input stream object.

Example: An illustration about opening a file using constructor

Output: