Input/Output Stream Example

#include <iostream.h>  // cin/cout streams
#include <fstream.h>   // file streams

void main(void)
{
  int value;
  ifstream myIn;       // declare an input file stream
  ofstream myOut;      // declare an output file stream

  // this "opens" the file on the disk for input
  myIn.open("datafile.in", ios::in);

  // this "opens" the file on the disk for output
  myOut.open("datafile.out", ios::out);

  myIn >> value;         // prime while loop
  while ( !myIn.eof() )  // at end of input file?
  {                     
    myOut << value;  // write value to output file
    myIn >> value;   // read next value from input file
  }
  myOut.close();     // close output file
  myIn.close();      // close input file
}
Note: This example assumes the input file contains only integers

Previous page
Next page

Back to Lesson 13 Index
Back to Outline