The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.

File Output

Sending output to a file is similar. For example, we could modify the previous program to copy lines from one file to another.

  ifstream infile ("input-file");
  ofstream outfile ("output-file");

  if (infile.good() == false || outfile.good() == false) {
    cout << "Unable to open one of the files." << endl;
    exit (1);

  while (true) {
    getline (infile, line);
    if (infile.eof()) break;
    outfile << line << endl;

Last Update: 2005-11-21