• Main Menu
  • File Input/Output

    In C++, file input/output services are implemented through a component header file available in the 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 data file, as an input file, we shall create a file stream object of input type i.e., ifstream type.

    ifstream inpt-file ("DataFile") ;

    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 channelized through the input stream object.

    Example: An illustration about opening a file using Constructor

    void main() 
        ofstream fout("student") ; //connect student file to output stream fout 
        char name[30], ch ; 
        float marks=0.0; 
        //Loop to get 5 records 
        for (int i=0; i<5; i++) 
            cout<< "nStudent " << ( I + 1 ) << " :tName :"; 
            cout << "ttMarks :"; 
            cin>> marks; 
            cin.get(ch);//to empty input buffer 
            //write to the file 
            fout<< name <<'n'<< marks <<'n'; 
        fout.close( ); //disconnect student file from fout 
        ifstream fin("student"); //connect student file to input stream fin 
        fin.seekg(0); // To bring file pointer at the file beginning 
        cout<< "n"; 
        for(i=0;i<5;i++) //Display records 
            fin.get(name,30); //read name from file student 
            fin >> marks; 
            fin.get(ch); //read marks from file student 
            cout << "Student Name : " << name; 
            cout << "tMarks : " << marks << "n"; 
        fin.close( ); //disconnect student file from fin stream 


    Student 1 :     Name    :jack
                    Marks   :45
    Student 1 :     Name    :mark
                        Marks   :87
    Student 1 :     Name    :james
                        Marks   :78
    Student 1 :     Name    :sean
                        Marks   :56
    Student 1 :     Name    :shane
                        Marks   :89
    Student name : jack     Marks : 45
    Student name : mark     Marks : 87
    Student name : james    Marks : 78
    Student name : sean     Marks : 56
    Student name : shane    Marks : 89

    Got Something To Say:

    Your email address will not be published. Required fields are marked *

    } 230 queries in 0.385 seconds.