Next: C File Handling Up: Handling Files in Previous: Handling Files in

UNIX File Redirection

UNIX has a facility called redirection which allows a program to access a single input file and a single output file very easily. The program is written to read from the keyboard and write to the terminal screen as normal.

To run prog1 but read data from file infile instead of the keyboard, you would type


  prog1 < infile
To run prog1 and write data to outfile instead of the screen, you would type

  prog1 > outfile
Both can also be combined as in

  prog1 < infile > outfile

Redirection is simple, and allows a single program to read or write data to or from files or the screen and keyboard.

Some programs need to access several files for input or output, redirection cannot do this. In such cases you will have to use C's file handling facilities.


January 1995