Opening a File for Input and Reading From the File

Use the following steps to open a file for input and to read from the file.

  1. Construct an fstream or ifstream object to be associated with the file. The file can be opened during construction of the object, or later.
  2. Use the name of the fstream or ifstream object and the input operator or other input functions of the istream class, to read the input.
  3. Close the file by calling the close() member function or by implicitly or explicitly destroying the fstream or ifstream object.

Constructing an fstream or ifstream Object for Input

You can open a file for input in one of two ways:

      #include <fstream.h>
      void main() {
         fstream infile1;
         ifstream infile2;
         infile1.open("myfile.dat",ios::in);
         infile2.open("myfile.dat");
         // ...
      }
      #include <fstream.h>
      void main() {
         fstream infile1("myfile.dat",ios::in);
         ifstream infile2("myfile.dat");
         // ...
      }

The only difference between opening the file as an fstream or ifstream object is that, if you open the file as an fstream object, you must specify the input mode (ios::in). If you open it as an ifstream object, it is implicitly opened in input mode. The advantage of using ifstream rather than fstream to open an input file is that, if you attempt to apply the output operator to an ifstream object, this error will be caught during compilation. If you attempt to apply the output operator to an fstream object, the error is not caught during compilation, and may pass unnoticed at runtime.

The advantage of using fstream rather than ifstream is that you can use the same object for both input and output. For example:

// Using fstream to read from and write to a file
#include <fstream.h>
void main() {
   char q[40];
   fstream myfile("test.x",ios::in); // open the file for input
   myfile >> q;                      // input from myfile into q
   myfile.close();                   // close the file
   myfile.open("test.x",ios::app);   // reopen the file for output
   myfile << q << endl;              // output from q to myfile
   myfile.close();                   // close the file
} 

This example opens the same file first for input and later for output. It reads in a character string during input, and writes that character string to the end of the same file during output. If the contents of the file text.x before the program is run are:

barbers often shave

the file contains the following after the program is run:

barbers often shave
barbers

Note that you can use the same fstream object to access different files in sequence. In the above example, myfile.open("test.C",ios::app) could have read myfile.open("test.out",ios::app) and the program would still have compiled and run, although the end result would be that the first string of test.C would be appended to test.out instead of to test.C itself.

Reading Input from a File

The statement myfile >> a reads input into a from the myfile stream. Input from an fstream or ifstream object resembles input from the standard input stream cin, in all respects except that the input is a file rather than standard input, and you use the fstream object name instead of cin. The two following programs produce the same output when provided with a given set of input. In the case of stdin.C, the input comes from the standard input device. In the case of filein.C, the input comes from the file file.in:

stdin.C filein.C
#include <iostream.h>
 
void main() {
   int ia,ib,ic;
   char ca[40],cb[40],cc[40];
   // cin is predefined
   cin >> ia >> ib >> ic
       >> ca;
   cin.getline(cb,sizeof(cb),'\n');
   cin >> cc;
   // no need to close cin
   cout << ia << ca
        << ib << cb
        << ic << cc << endl;
   }
#include <fstream.h>
 
void main() {
   int ia,ib,ic;
   char ca[40],cb[40],cc[40];
   fstream myfile("file.in",ios::in);
   myfile >> ia >> ib >> ic
          >> ca;
   myfile.getline(cb,sizeof(cb),'\n');
   myfile >> cc;
   myfile.close();
   cout << ia << ca
        << ib << cb
        << ic << cc << endl;
   }

In both examples, the program reads the following, in sequence:

  1. Three integers
  2. A whitespace-delimited string
  3. A string that is delimited either by a new-line character or by a maximum length of 39 characters.
  4. A whitespace-delimited string.

Note that, when you define an input operator for a class type, this input operator is available both to the predefined input stream cin and to any input streams you define, such as myfile in the above example.

All techniques for reading input from the standard input stream can also be used to read input from a file, providing your code is changed so that the cin object is replaced with the name of the fstream object associated with the input file.