[Homepage] | [Next: Submitting Homework] | [Up: Creating Output] | [Prev: Using script]

Redirecting Output (>) and Input (<)

A better way to create output files is to use redirection. You can redirect the output of any command to a file using >. For instance, typing prog > outputFile sends all of the printf statements in the program to outFile. Note that if prog were printing a prompt like Enter number of miles> and then waiting for the user to enter a number, the prompt is sent to the output file and you won't ever see it on the screen!
Let's assume you wrote a program called ``hello'' which prints ``Hello World'' to the screen. To generate an output file, type ``hello > hello.out''. Using the ''>'' is called file re-direction. You are redirecting the output of hello to the file hello.out. If you examine the file hello.out (using your favorite editor), you will find that it contains exactly what would have been normally printed to the screen by your program.

File redirection also also allows you to redirect input as well: someProgram < inputFile causes any scanfs to read from inputFile instead of the keyboard.


Ewerton L. Madruga
Modified by Kevin Klenk