Output Stream Example

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

void main(void)
{
  ofstream myOut;     // declare an output file stream

  // associate the output stream with a disk file.
  // this "opens" a file on the disk named datafile.out
  // we can now write to the file
  myOut.open("datafile.out", ios::out);

  for (int x = 1; x <= 5; x++)
  {
    myOut << setw(5) << x;
    myOut << setw(5) << x * x;
    myOut << setw(5) << x * x * x;
    myOut << endl;
  }
  myOut.close();     // close the output file
}

Output: (in the file named datafile.out)
    1    1    1
    2    4    8
    3    9   27
    4   16   64
    5   25  125
Previous page
Next page

Back to Lesson 13 Index
Back to Outline