Is it possible to cout to terminal while redirecting cout to outfile?

bash, c++, cout, redirect, shell

Solution

You should use cerr to output your help message to STDERR, which is not included in your redirection to outfile.o.

Given `./program < infile.in > outfile.o`:

cout << "This writes to STDOUT, and gets redirected to outfile.";
cerr << "This doesn't get redirected, and displays on screen.";

If, later on, you want to redirect both STDOUT and STDERR, you can do

./program < infile.in &> outfile.o

If you want to redirect only STDERR, but allow STDOUT to display, use

./program < infile.in 2> outfile.o

Bash redirection is more complex than most people realize, and often everything except the simplest form (">") gets overlooked.

Problem

I'm running a program and redirecting `cout` to an outfile, like so: ``` ./program < infile.in > outfile.o ``` I want to be able to read in an option ('-h' or '--help') from the command line and output a help message to the terminal. Is there a way I can do this but still have the regular `cout` from the rest of the program go to the outfile? Would `cout` be the right object to use for such a thing?

Original source