By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,022 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,022 IT Pros & Developers. It's quick & easy.

freopen on both stdout and stderr

P: n/a
Hello all,
a while ago I was pointed towards freopen as a way to redirect stderr to a
log file. It works great, but apparently the app also writes a few lines to
stdout. Now I could redirect to 2 separate files, but would rather keep the
2 flows together.

Is it correct to do this:
stderr=freopen(LogFile, "w", stderr);
stdout=freopen(LogFile, "a", stdout);

I can see plenty of reasons why it would fail (buffering and flushes come to
mind).
Any advice ?
--
Guillaume Dargaud
http://www.gdargaud.net/Climbing/
"Faith can move mountains but let them happily fall down on the heads of
other people. What's the point in moving mountains when it's so simple to
climb over them ?" - Boris Vian, surrealist French writer and singer, En
verve.
Nov 23 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Guillaume Dargaud wrote:
Hello all,
a while ago I was pointed towards freopen as a way to redirect stderr to a
log file. It works great, but apparently the app also writes a few lines to
stdout. Now I could redirect to 2 separate files, but would rather keep the
2 flows together.

Is it correct to do this:
stderr=freopen(LogFile, "w", stderr);
stdout=freopen(LogFile, "a", stdout);
It's not in accordance with the standard to do this, as "stderr" and
"stdout" are not, as I recall, guaranteed to be lvalues...

If you can assign them, and you're prepared to accept non-portability,
then you could perhaps try the following, untested, hackery

stderr=freopen(LogFile, "w", stderr);
fclose(stdout);
stdout=stderr;

Otherwise, I'm not convinced you have many options available to you.
Nov 23 '07 #2

P: n/a
On Nov 23, 6:01 am, Mark Bluemel <mark_blue...@pobox.comwrote:
Guillaume Dargaud wrote:
Hello all,
a while ago I was pointed towards freopen as a way to redirect stderr to a
log file. It works great, but apparently the app also writes a few lines to
stdout. Now I could redirect to 2 separate files, but would rather keep the
2 flows together.
Is it correct to do this:
stderr=freopen(LogFile, "w", stderr);
stdout=freopen(LogFile, "a", stdout);

It's not in accordance with the standard to do this, as "stderr" and
"stdout" are not, as I recall, guaranteed to be lvalues...

If you can assign them, and you're prepared to accept non-portability,
then you could perhaps try the following, untested, hackery

stderr=freopen(LogFile, "w", stderr);
fclose(stdout);
stdout=stderr;

Otherwise, I'm not convinced you have many options available to you.
N1256 seems to specify that, on success, freopen() just returns its
third argument, so there'd be no need to assign the result to stdout/
stderr when passing them as the stream. There's even a footnote:

"The primary use of the freopen function is to change the file
associated with a standard text stream (stderr, stdin, or stdout), as
those identifiers need not be modifiable lvalues to which the value
returned by the fopen function may be assigned."
Nov 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.