Unix recognizes three standard input and output streams, called
standard input (stdin), standard output (stdout) , and standard error
(stderr). Normally standard input is taken from the keyboard and
standard output and error go to the screen. With iostream.h
we access these streams through the standard names cin, cout, and cerr, respectively. There is also a clog
associated with the standard error stream.
| Unix | iostream | default |
| stdin | istream cin | keyboard |
| stdout | ostream cout | screen |
| stderr | ostream cerr | screen |
| stderr | ostream clog | screen |
myprog > outfileNow suppose you wanted to your program to prompt the user for some information, or you wanted the user to see an error message. Such output really should go to the screen instead of to the file outfile where the user wouldn't notice it right away. The Unix solution is to provide two output streams, stdout and stderr, and declare that stdout gets redirected to the file in the command above, but stderr gets sent to the screen.
For example, in your C++ code you would write
cout << data[i] << endl; cerr << "There is an error" << endl;and the data value would then be redirected to outfile while the error message would appear on the screen. Without redirection everything goes to the screen.
One shouldn't be misled by the name cerr, which implies that it is used only for error messages. It is quite proper to use the cerr stream to prompt the user for input. The stream clog could also be used for that purpose.