I/O Redirection and the Beauty of the Command Line

CS 321 2007 Lecture, Dr. Lawlor

Say your program reads input from cin, like
#include <string>
#include <iostream>

int main() {
std::string s;
while (std::cin>>s) {
std::cout<<"Read value: "<<s<<"\n";
}
return 0;
}
What if you get tired of typing stuff into a program like this?  Or what if you want to automate the execution of that program to run 10,000 times?

Input Redirection

There's something rather magical you can do in a command-line shell called "redirection".  Any program that reads from standard input (stdin, cin, or file descriptor 0) can be made to read from a file instead using the less-than character, like so:
C:\> reads < pile.txt 
Read value: Welcome
Read value: to
Read value: the
Read value: pile!
...
You can think of the less-than as "injecting" the file's contents into the program.  This is called "I/O redirection", and it works the same at a DOS prompt or in a UNIX shell--it's actually a universal of command-line programs!

On UNIX, you can feed a program a stream of random binary junk with "someprogram < /dev/urandom".  This is actually useful for security and robustness testing (called "fuzz testing").

Output Redirection

You can also redirect *output* to a file with greater-than:
C:\> dir > sillylist.txt
This stores the directory listing that "dir" prints out to the file "sillylist.txt".  Everything that dir prints (via std::cout, stdout, or file descriptor 1) will go into the file!

Pipe

You can also redirect one program's output to be the input of another program:
C:> dir | more
This is called a "pipe"--it's shift-backslash on most keyboards, right under the backspace key.  The output of "dir" is "piped" to the input of the command "more".

All this stuff works in DOS, Windows, or any UNIX, but UNIX machines have tons of interesting command-line utilities that are designed to work with I/O redirection.  You can get a version of these tools on a standard Windows machine by installing the package cygwin.
There are hundreds of little utilities like this in UNIX, and a skilled operator can string them together to get suprisingly useful behavior with very little effort!  Another nice aspect of this particular program design is that it works great with programs you wrote yourself using ordinary cin and cout.