CS 107 - Lab 8

File Input and Output

We have looked at reading information from a file using redirection of the input and storing the results into a file using redirection of the output. In C++, we can read and write directly from/to a file and still have access to cin to read from the keyboard and to cout to write to the monitor. Chpater 12 in the book covers this information.

The library fstream contains the needed information to allow for true file I/O. This file contains two classes

When variable created as an instance of theses classes allow us to read from or write to a different file. Thus we can read from and write to as mamny files as we want. To declare a instance/varaible of these classes type:
     ifstream infile;
     ofstream outfile;
The variable names could be anything that you want. After the variable are declared, we must open the file. Opening of the file connects the variable in your program with the actual file on the computer. The method open() of the ifstream and ofstream classes take one parameter which is the name of the file. This name is given as a string. To open the file lab8a.data for reading we would use the code:
     infile.open("lab8a.data");
If we wanted the user to type in a filename and open that file, we can follow the example shown on pp. 517 and 518. This example needs us to create a character array which will hold the value entered by they user and then use this array as the parameter of the open() method.
     char fileName[30];   // character array to hold the filename
     
     cout << "Enter the name of the file to read from: ";
     cin >> fileName;
     infile.open(fileName);
To open a file for writing, follow the same steps as was used for reading except make sure the variable is of type ofstream instead of ifstream.

Reading from the File

We can use the >> operand to read from a file just like it is used to read from cin. We can use the << operand to write to a file just like it was used to write to cout. Instead of using cin or cout, we use the variable name. Thus to read from the file opened with the variable infile, we could use the code:
     infile >> val1;
To write to the file opened with the variable outfile, we could use the code:
     outfile << "The result is: " << val << endl;

Closing a File

When you are finished using a file, you should always close the file. This disconnects the file on the computer from your program. This is done by the use of the close() method. The method is used the same for both the ifstream and ofstream classes. The method does not take any parameters and is used as follows:
     infile.close();
     outfile.close();

Other Useful Methods

You should always check whether the file was succesfully opened. This is done by the use of the fail() method. See page 509. This method takes no parameters and return a true value if the file was not successfuly opened. This method is often used with an if statement as follows:
     infile.open ("lab8a.data");
     if (infile.fail() == true)
        {
         cout << "Opening file lab8a.data for input failed.  Goodbye" << endl;
         exit(1);
        }
The above code will quit the program after printing an error message if the file failed to open.

When reading input from a file, we often read until the end of the file is reached. This can be checked by using the eof() method. This method takes no parameters and returns a true value if the file has had everything read from it. See page 513. This use of this method can be shown in the following code which will copy a file:

     ifstream infile;
     ofstream outfile;
     char ch;

     infile.open("original.txt");
     outfile.open("copy.txt);

     // check for successful opening is omitted

     infile.get(ch)
     while (infile.eof() == false)
        {
         outfile << ch;
         infile.get(ch);
        }

     // closing of files is omitted

Lab Assignment

Due: By your Lab Time during the tenth week of the semester (the week of 3/24/2003).

You may turn in the assignment to your TA during lab or place it in his mailbox in 905 SEO. It is suggested that you place it in his mailbox just in case you are unable to attend lab. You are to hand in a print out of your program to do the following to your TA.

The following data files can be used to help test your program:

Hand in a print out of your program to your TA.