difference between cout and cerr

Other than the theoretical difference between cout and cerr where the former puts values to the monitor and the latter puts values related to errors to the monitor, is there any real difference here? Why not use cout when you want to send anything to monitor? Why use cerr at all?
Neither of them necessarily output to the monitor. They're both abstract streams which can be mapped to different locations.

For example, you might have cout going to the console (monitor), whereas cerr might be going to a log file. Or both might be going to different log files.


Where cout actually goes can vary depending on the program and how the program is executed. For example if you run your program from the commandline:

myprogram.exe

Things sent to cout will be printed in the console. However if you run it from the commandline like so:

myprogram.exe > out.txt

Things sent to cout will be written to out.txt.


(EDIT: this is a Windows example, anyway -- *nix can do similar things but it's probably slightly different syntax)
Last edited on
You can redirect any of the iostreams that you want to, std::cerr is only set to the monitor by default. So if you want to view errors while you're debugging but you want the errors to go to some file for the release mode you only need to change it in one place and it won't interfere with std::cout. That's how I use it at least
Last edited on
Here's a (quick and dirty) translation from the german cplusplus forum:
"std::cout is a buffered stream which means that not every character appears on the console immediately but is buffered for a better performance.

std::cerr is not buffered, hence it's suitable for error messages, e.g. when a program gets stuck and tries to give all the output it can until it freezes."

I've read somewhere about compilers or IDEs that print cout and cerr onto the console in debug mode but omit the cerr stream in release mode, or something like that. I don't remember the details.
Last edited on
OOoo... that's really cool, SmeeAfshin. I didn't realize cerr was unbuffered. That makes a lot of sense. Nice.
@Computergeek01: ┬┐you make the redirection inside the code?


http://www.tldp.org/LDP/abs/html/io-redirection.html

> omit the cerr stream in release mode
quite a bad decision
Last edited on
@ ne555: The redirection pipes are a valid option, but if I know that 90% of the time I want the errors to be logged to a file then why wouldn't I just hard code that? The pipes work the other way to.
Disch, I don't care much about the topic, I just remembered I had read st about it recently and retrieved it to help merlino. If you do care, double-check it and confirm or rectify it yourself.
Topic archived. No new replies allowed.