CIS 1.5          Sending output to a disk file

 

Below is a program that sends all of its output to the screen.  Then there is a modified version that sends some output to a file and some to the screen.  The changes that must be made for this modified version are marked by arrows. 

 

// original version

#include <iostream>

using namespace std;

 

int main()

{

    cout << "line 1" << endl;

    cout << "line 2" << endl;

 

    system("PAUSE");

    return 0;

}

 

 

 

// modified version

#include <iostream>

#include <fstream>                     ß

using namespace std;

 

int main()

{

    ofstream output("output.txt");     ß

 

    output << "line 1" << endl;        ß

    cout << "line 2" << endl;

 

    output.close();                    ß

    system("PAUSE");

    return 0;

}

 

Where does the message saying line 1 go?  Where does the message saying line 2 go?  How can we make line 1 and line 2 both go to the file?  How can we make them both go to the screen?