How to redirect CERR?

I

Immortal Nephi

I am still examining C++ Standard Library in MSDN. It does not
provide me enough information. CERR gives you the option to use
either stdout or file. CERR always redirects message to the console s
creen. How do I tell CERR to redirect message to the file directly?
I know how to write fstream code, but I am not sure how CERR can use
file instead.
Can you please write your example code here? The code allows to give
you two choices. Choose console screen or file to write message.
 
M

Maxim Yegorushkin

On 14/02/10 17:18, Immortal Nephi wrote:

[]
> CERR gives you the option to use either stdout or file.

A stream gives you an option of formatting. Where the formatted output
goes to is up to streambuf.
> CERR always redirects message to the console s
creen. How do I tell CERR to redirect message to the file directly?
I know how to write fstream code, but I am not sure how CERR can use
file instead.
Can you please write your example code here? The code allows to give
you two choices. Choose console screen or file to write message.

Use ios::rdbuf() function to redirect. See example
http://www.cplusplus.com/reference/iostream/ios/rdbuf/
 
J

James Kanze

I am still examining C++ Standard Library in MSDN. It does
not provide me enough information. CERR gives you the option
to use either stdout or file. CERR always redirects message
to the console s creen. How do I tell CERR to redirect
message to the file directly?

Both cout and cerr are "pre-opened" by the implementation. What
they write to is implementation defined. Both Unix and Windows
allow the invoking program to set them up to pretty much
anything you want.
I know how to write fstream code, but I am not sure how CERR
can use file instead.

If you're using cerr, it's because *you* don't want to choose
the destination. You expect the invoking program to have set it
up the way that it wants, and you expect to do what it wants.

If you want to force output to file from within your program,
you should be using ofstream. (It's possible to change the
output sink of cerr, but I would consider it more obfuscation
than anything else.)
 
J

James Kanze

A program can capture and redirect its own standard error
channel as well, but this is something you normally don't want
to do (unless you are implementing something like the Apache
web server).

Just curious, but why would something like Apache want to modify
the destination of cerr from within itself. (I've written a lot
of large scale servers, and they've never used cerr, because it
would typically be attached to /dev/null.)
 
J

James Kanze

[...]
[...]
I am not sure, but maybe this is done because Apache can run
foreign CGI- style extensions in-process for better
performance, so it does not want to lose any messages which
they might output to stderr.

Ah, yes. They want to protect themselves against plugin's which
do the wrong thing.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top