468,769 Members | 2,252 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,769 developers. It's quick & easy.

freopen on both stdout and stderr

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
2 7869
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
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.

Similar topics

8 posts views Thread by Hugh Macdonald | last post: by
6 posts views Thread by Tsai Li Ming | last post: by
3 posts views Thread by Laszlo Zsolt Nagy | last post: by
7 posts views Thread by Andre | last post: by
2 posts views Thread by Alex Vinokur | last post: by
3 posts views Thread by John Devereux | last post: by
5 posts views Thread by Wesley Henwood | last post: by
2 posts views Thread by Massi | last post: by
13 posts views Thread by Vincent Delporte | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.