Notice! This document is currently in Archived status.
The content of this document may be incorrect or outdated.

Print this article Edit this article

Redirecting input-output

It is often desirable to route program input and output from files instead of the terminal. A program which uses standard I/O devices (5 & 6), may be used to read data stored in a file called data, rather than the screen. The command is executed using the redirect symbol (<) as shown below:
	 a.out < data
All device 5 read statements are directed to the file data instead of the keyboard. Notice that all device 6 output will default to the terminal screen. The source program and data should be in separate files (data files should neither be compiled nor linked). Similarly, to route the output to a file called outdata, another type of redirect may be used:
	 a.out > outdata
The file outdata will be immediately created if it doesn't exist. If there is such a file, it will be replaced by a new copy of outdata. Again, all read statements on device 5 default to the screen, thus the input has to be supplied interactively.

The disadvantage of this method is that the program will not write to the screen prompting for input; hence the user will not know when input is expected. This is solved by combining the redirect commands as shown:

	 a.out < data > outdata

Last Modified: Dec 19, 2016 11:12 am US/Eastern
Created: Jun 21, 2007 12:37 pm GMT-4 by admin
JumpURL: