File I/O:  INPUT

There are six steps involved in reading information from the file

1.      Need to include an appropriate header file

2.      Declare an input file stream

3.      Open the file (and connect it to the declared input stream)

4.      Check whether the file opened successfully

5.      Read data from the file

6.      Close the file

1.                  To read data from an input file and write to an output file (use file I/O) you need to include the library fstream in your program:

    #include <fstream>

2. Declaring input file stream object.
 
We read data from a file using an input file stream (or ifstream).  We must declare an input file stream object:

    Syntax:    ifstream InputFileStreamName;
 
Examples of declaration:

                        ifstream infile;
                        ifstream fin;
                        ifstream input_file;
 
(Think of ifstream as a new datatype. The format is the same as in declaring:    int  num;)

3. Openning the file.
After declaring the input file stream object, we can open the file using open( ) function:
    infile.open("name of file to open");

      This links the name you gave to the input data stream to an external file.

      Make sure you use the name you declared (in this case – infile).

Examples:

a)  Hard-coding the file name (name of the file is known prior to compilation).

     infile.open("test.dat");  // this opens the file test.dat so we can read the data in it.

·        If a file is located in the same directory as the source file, you just need to provide the name.

·        Don’t forget to place double quotes around the file name.

b)   Sometimes you need to specify the file name at the run-time. You need to declare a character array (we will talk about what it means later in great detail). Then you can read the file name from the keyboard input.

ifstream  input_file;
char file_name[20];  //will hold a file name of 19 characters max
cout<<”Please enter file name:”;
cin >>file_name; // the user will provide the file name
input_file.open(file_name);  //don’t need quotes around the file name in this case.
 

4.  Before you use the file, you should check if the file opened successfully. Don’t ever just assume that it’s there and ready to be used.

You need to use ‘if’ statement to check the input stream – we will come back to this step next week.

      if (!infile)
            cout << “Could not open input file\n”;

 ·        A 1 is returned if the input stream can be used, otherwise the value is 0.

·        A zero value is also associated with an input stream that has no more data to extract.  If we are at this point we say that the stream has reached end of file or eof.

5. Reading from the file

Now we are ready to extract data from an input file -- we do it similarly to using cin:
We would read input for three variables j, k and i from the keyboard using cin:

    cin >> j >> k >> i;

or we would read input from the input file using our defined infile :

    infile >> j >> k >> i;

6.  Closing the file

Closing the file is done using close( ) function. This breaks the link between your data file and the input file stream object. You should always do it after you are done using the file. Do not put anything inside the parentheses.

infile.close( );