std::cerr

D

Dylan

What advantages does std::cerr offer over std::cout?

(I'm trying to understand why it exists)

thanks
 
L

Lionel B

Dylan said:
What advantages does std::cerr offer over std::cout?

There is no "advantage".
(I'm trying to understand why it exists)

It is frequently convenient to be able to separate "normal" from "abnormal" output. Thus one common use of cerr is to
log errors: i.e. one might (re)direct output to stderr (but not to stdout) to an error log, which might be a file, a
terminal screen or (my preferred option) a tannoy that squawks "ERROR! ERROR! ILLEGAL MEMORY ACCESS AT 0x038af2e2!
MISSION ABORT!".
 
D

Dylan

There is no "advantage".


It is frequently convenient to be able to separate "normal" from "abnormal" output. Thus one common use of cerr is to
log errors: i.e. one might (re)direct output to stderr (but not to stdout) to an error log, which might be a file, a
terminal screen or (my preferred option) a tannoy that squawks "ERROR! ERROR! ILLEGAL MEMORY ACCESS AT 0x038af2e2!
MISSION ABORT!".

hehe. So how do I redirect the output of std::cerr, say, to a file?
 
R

roberth+news

| What advantages does std::cerr offer over std::cout?

On my OS, a common thing to do is to make programs reading from cin and
writing to cout. If foo and bar are two such programs, and I wrote

foo < infile | bar > outfile

then foo would have cin connected to the file infile, and its cout
connected to bar's cin. Bar's cout would be connected to the file
outfile.

If any of there is an error, an error message written to cerr would
appear on the screen immediately.

Cerr is not buffered, though, so excessive usage might be ineffient. If
you rather want to write information to the screen, there is also a
buffered version called clog.

summary:
cin buffered, connected to stdin
cout buffered, connected to stdout
cerr unbuffered, connected to stderr
clog buffered, connected to stderr.
 
P

Prawit Chaivong

| What advantages does std::cerr offer over std::cout?

On my OS, a common thing to do is to make programs reading from cin and
writing to cout. If foo and bar are two such programs, and I wrote

foo < infile | bar > outfile

then foo would have cin connected to the file infile, and its cout
connected to bar's cin. Bar's cout would be connected to the file
outfile.

If any of there is an error, an error message written to cerr would
appear on the screen immediately.

Cerr is not buffered, though, so excessive usage might be ineffient. If
you rather want to write information to the screen, there is also a
buffered version called clog.

summary:
cin buffered, connected to stdin
cout buffered, connected to stdout
cerr unbuffered, connected to stderr
clog buffered, connected to stderr.

Wow great!!. What's your OS?
 
R

roberth+news

| (e-mail address removed) wrote:
| > | What advantages does std::cerr offer over std::cout?
| >
| > On my OS, a common thing to do is to make programs reading from cin and
| > writing to cout. If foo and bar are two such programs, and I wrote
| >
| > foo < infile | bar > outfile
| >
| > then foo would have cin connected to the file infile, and its cout
| > connected to bar's cin. Bar's cout would be connected to the file
| > outfile.
| >
| > If any of there is an error, an error message written to cerr would
| > appear on the screen immediately.
| >
| > Cerr is not buffered, though, so excessive usage might be ineffient. If
| > you rather want to write information to the screen, there is also a
| > buffered version called clog.
| >
| > summary:
| > cin buffered, connected to stdin
| > cout buffered, connected to stdout
| > cerr unbuffered, connected to stderr
| > clog buffered, connected to stderr.
|
| Wow great!!. What's your OS?

None of importance, really; the four objects are standard in C++, but
I use Crux <URL: http://crux.nu>, Solaris <URL: http://www.sun.com> and
Redhat Enterprise Linux <URL: http://www.redhat.com/> on a regular
basis.

To another question in this thread: I can redirect stderr to a file
with

foo 2> errmsg

(for a program foo and a file errmsg.)
 
J

James Daughtry

hehe. So how do I redirect the output of std::cerr, say, to a file?
The answer is very OS and compiler implementation specific

Really? Re-assigning stream buffers is a portable operation:

#include <fstream>
#include <iostream>

int main()
{
std::eek:fstream log("somefile");
std::streambuf *save = std::cerr.rdbuf();

// Redirect stream buffers
if (log.is_open())
std::cerr.rdbuf(log.rdbuf());

std::cout << "This is a test message\n";
std::cerr << "This is a test message\n";

// Restore cerr's stream buffer before terminating
if (log.is_open())
std::cerr.rdbuf(save);
}
 
R

red floyd

Lionel said:
There is no "advantage".




It is frequently convenient to be able to separate "normal" from "abnormal" output. Thus one common use of cerr is to
log errors: i.e. one might (re)direct output to stderr (but not to stdout) to an error log, which might be a file, a
terminal screen or (my preferred option) a tannoy that squawks "ERROR! ERROR! ILLEGAL MEMORY ACCESS AT 0x038af2e2!
MISSION ABORT!".

Also, cerr is also unbuffered, whereas cout is buffered. clog exists,
and (as I understand it) is a buffered version of cerr.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top