USING I/O REDIRECTION


I/O redirection provides a mechanism for retrieving data
from a file, while your C program believes that the data
is actually coming from the standard input device (stdin),
or in other words the keyboard.  The UNIX shell handles
the redirection process; the program is unaware that the
shell has redirected the input source from the keyboard to
a file.

Thus we can write our programs using simple input statements
such as scanf and getchar, which presumably draw their 
input from the keyboard.  But in reality the data is coming
from a file.  So we obtain the benefits of batch processing
while using simple code.  

When using input direction, ALL input will come from the 
designated file.   Thus redirection is an "all-or-nothing"
technique in the sense that you cannot draw some of the
input from this designated file and some from the keyboard.
If multiple input sources are needed, it is best to work with
FILE pointers.

To use I/O redirection for the purpose of input, use the
basic input statements such as scanf and getchar, but
recognize that the input is not truly interactive.  This
user prompts such as "Enter data now" are not needed. 
Also, such things as using loops to have a user repeat an
entry in the case of a mistake is not used, as in reality
there is no user interacting with the pyour program.

Compile your program in the normal manner.  When executing
the program, type:  a.out < inputfile.txt
You can test the program by typing a few lines of input 
from the keyboard by executing your program just by
typing: a.out
But remember that there will be no prompts to guide you.

To direct output, use the basic output statements such as
printf, but remember that the output will not appear on the screen.
Instead, it will end up in the file you specified.  To
execute, type: a.out > out.txt

The two can be combined by typing:  a.out < input.txt > out.txt