Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use cerr and when cout in C++?

I am looking for an example that differentiates between cerr and cout in C++?

When do I need to use cerr?

like image 918
kenway Avatar asked Oct 04 '14 09:10

kenway


People also ask

What is the difference between CERR and cout?

cout is an object of the stdout stream, while cerr is an object of the stderr stream. stdout and stderr are different streams, even though they both refer to console output by default. Redirecting (piping) one of them (e.g. program.exe >out. txt) would not affect the other.

Is CERR faster than cout?

cout is buffered, cerr is not, so cout should be faster in most cases. (Although if you really care about speed, the C output functions such as printf tend to be a lot faster than cout/cerr). cout and cerr are ostream objects.

What is CERR used for?

The cerr object in C++ is used to print error messages. It is defined in the iostream header file.

Why do we use CERR in C++?

The “c” in cerr refers to “character” and 'err' means “error”, Hence cerr means “character error”. It is always a good practice to use cerr to display errors.


3 Answers

Many operating systems let you redirect input and output from/to files. When end-users redirect your output to a file, end-users do not see anything that you write to cout; if you want your output to be seen by end-users, you need a separate stream to which you print messages for them.

Suppose you are writing a program that reads from the standard input line-by-line, and writes these lines to the standard output in sorted order. Let's say that your program takes a command-line parameter that says if the output needs to be sorted in ascending or descending order. If end-users pass an invalid value for this parameter, you want to print a message "Invalid flag" to the console. Printing it to cout would be incorrect, because cout could be redirected to a file, so the users would not see it. The correct solution in this situation is to write this message to cerr.

like image 193
Sergey Kalinichenko Avatar answered Sep 20 '22 16:09

Sergey Kalinichenko


Quite often the user of your program is interested only in results, because these are printed to stdout, for instance, if you use a unix command cat, eg:

$ cat file.txt

You're expecting file.txt contents to appear on stdout. However, if anything happens during the execution of cat (strictly theoretically speaking, nothing has ever happenned to me), you'd expect it to go to stderr, thus, as a user, you're still able to separate the two, eg:

$ cat file.txt 1>result.txt 2>stderr.txt

Suppose I want to collect contents of multiple files, I do the following

$ cat *.java 1>all_files_conent.java 2>errors.txt

In case any of the files isn't accessible (eg. because of permissions), errors.txt will have appropriate message:

cat: Controller.java: Permission denied

But the contents of all_files_content.java are as correct as they can be.

Therefore, if the message is an actual product of your program, you should use cout, if it's just a status message, use cerr. Of course, all this doesn't matter that much if what goes to the console is just a byproduct. However, you might still want to allow user to separate the two, as in the example above.

like image 44
lisu Avatar answered Sep 22 '22 16:09

lisu


std::cout : Regular output (console output)

std::cerr : Error output (console error)

Google is your friend :)

like image 23
Max Avatar answered Sep 20 '22 16:09

Max