Date: Mon, 11 Nov 1996 17:01:17 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Tue, 01 Oct 1996 20:08:43 GMT Content-length: 4065
/usr/home/jdoe# g++ -o prog1 prog1.cpp /usr/home/jdoe# script Script started, file is typescript /usr/home/jdoe# prog1 Welcome to John Doe's program ... ... etc ... /usr/home/jdoe# exit Script done, file is typescript /usr/home/jdoe# lpr -Pmylaser typescript
Warning: If you use the script command, don't forget to type exit when you are done! And don't "nest" script commands.
prog1 >output.txtAlso, you can "append" the output of your program to a previous run with two greater-than signs:
prog1 >>output.txt
This has two disadvantages, however. First, since the output is going to a file, you can't see it on the screen! So you have to "know" what the expected input is beforehand. Second, the input you type is not redirected to the file. Thus you need to modify your program to "echo" the input so that it appears in the file too. For example:
#include <iostream.h> int main () { int num_apples; cout << "How many apples do you want?"; cin >> num_apples; cout << num_apples << endl; // ECHO INPUT FOR REDIRECTION return 0; }
The idea is to modify the program so that all the output goes to a file instead of a screen. Thus this method suffers the same problems as the DOS method above: output is not displayed on the screen (i.e. you can't see the prompts) and input is not redirected to the file (thus you have to echo the input in your program). For example, consider the following program:
#include <iostream.h> int main () { int num_apples; cout << "How many apples do you want?"; cin >> num_apples; return 0; }
To make this program's input and output go to a file named "output.txt", we could modify it like this (note the extra #include directive):
#include <iostream.h> #include <fstream.h> int main () { ofstream fout = "output.txt"; // Open a file for writing cout = fout; // Redirect cout to the file int num_apples; cout << "How many apples do you want?"; cin >> num_apples; cout << num_apples << endl; // Echo input for redirection return 0; }
Once you have the program's output saved in a file, you can use almost any means to print that file out. Under Windows you can use the Notepad, for example.