Lexington High School Mathematics Math Dept. Front Page  | Student Links  | Family Links 
C++ courses Intro. Prog. I  | Intro. Prog. II  | AP Comp. Sci.  | C++ Info Page 

File Streams Reference Sheet (Advanced Version)

So far we have read in all information from the console (screen) and written everything to the console (screen). It is also important to be able to read information from, and write it out to files. There are a number of steps which must be accomplished when dealing with files:

  1. fstream variables    - you must declare variables of this type to hold the link between the program and the file.
  2. opening the file      - when you open files you must specify whether you are going to read them or write to them.
  3. reading and writing - this can be accomplished by using the fstream variables in place of cin or cout or with getline.
  4. closing the file        - this should be done as soon as you are done with the file to free it up for any other users.

When opening files to write to them, you have a few options available that will position you within the file and also protect any existing files if you do not want to overwrite them. To use these options, link them together with |. For example, to open an existing file and append new information at the end, you might use:

ios::out | ios::nocreate | ios::app

ios::out open file for output
ios::app always write at the end (append)
ios::ate start writing at the end (at end)
ios::nocreate fail if the file does not exist
ios::noreplace fail if the file already exists

Below is a program which opens two files. From the first file it reads some names. It uses these names to write a sentence in the second file. The two files are shown after the code. You should note that when this code is run, nothing happens on the screen. There are no messages telling you that it is doing what you want unless you put some cout statements in your code telling you what is going on.

// Program to test file I/O

#include <iostream.h>    // regular I/O header
#include <fstream.h>     // file I/O header
#include "apstring.h"

int main()
{
   fstream infile, outfile;     // variables to hold the link to the file
   apstring name;               // name data read in from the file
   bool first = true;           // test if first name read from file

   // Open the input and output files
   infile.open("names_data", ios::in);  // names_data is opened for input
   outfile.open("hello", ios::out);     // hello is opened for output

   // Start putting data in the output file
   outfile << "Hello, ";

   while (!infile.eof())       // keep reading input file until a problem
   {
      infile >> name;          // read a name off a line of the input file
      if (name != "")          // ignore any blank lines
      {
         if (first)            // special output if the first name
         {
            outfile << name;
            first = false;     // no longer the first name
         }
         else                  // general output form
            outfile << " and " << name;
      }
   }

   outfile << "!" << endl;     // punctuate the sentence in output file

   // Close the input and output files
   infile.close();
   outfile.close();

   return 0;
}
Contents of names_data: Contents of hello:
   
Olivia Hello, Olivia and Carlo and Evan and Danny!
Carlo  
Evan  
Danny  

Use the example code as a guideline but remember that it is an example. It is not necessary to mimic the structure or the comments in your own code.