Basic input using cin


Use of input

Using files for input in Unix

Files for storing input

From the command line in Unix (i.e. when you are working in a terminal or dtterm window, or using ssh) you can use a file of data to provide input to a program instead of retyping the data each time.

This is handy if you have test cases you want to retry every time you alter the source code. Put the test cases in a file, and use the < symbol to direct the file data into your program:

csciun1.mala.bc.ca< myprogram < mydatafile
(Assuming your executable file is named myprogram and your test data is in file mydatafile) Note that when doing this the program doesn't take any input from the keyboard, it gets it all from the data file, so your data file must contain all the appropriate user responses in the correct order.

Also, the output is still printed to the monitor.

Files for storing output

Similarly, you can redirect the output from a program so that it is stored in a file instead of printed on the screen.

To do so, use the > symbol:

csciun1.mala.bc.ca< myprogram > resultsfile
If you wanted to use one file for input and store the results in another file, you can combine the two commands, e.g.
csciun1.mala.bc.ca< myprogram < mydatafile > resultsfile
Using programs to provide input for programs

Finally, if you want to directly use the output from one program as the input to another, you can use a pipe symbol | to connect the two:

csciun1.mala.bc.ca< generatorprogram | userprogram