Resetting redirected streams

  • Thread starter Michael W. Hicks
  • Start date
M

Michael W. Hicks

Okay, this is really a C question, not a C++ one, but cut me a little slack
please. :)

Anyway, considering one redirects a standard stream with, say, the following
code:

FILE *fp = freopen( "test.txt", "w", stdout );
puts( "This is a test." );

How would one reset stdout such that it writes data to the console?

TIA,
Mike
 
D

David Harmon

Okay, this is really a C question, not a C++ one, but cut me a little slack
please. :)

Wot, got toasted over on comp.lang.c? Unfortunately, what you are
asking is outside the standardized boundaries of either language. The
following has been known to work on various platforms and is IMO as
portable as you can get on this.
Anyway, considering one redirects a standard stream with, say, the following
code:

int save_stdout = dup(fileno(stdout));
FILE *fp = freopen( "test.txt", "w", stdout );
puts( "This is a test." );
How would one reset stdout such that it writes data to the console?

dup2(save_stdout, fileno(stdout));
close(save_stdout);
 
M

Michael W. Hicks

David Harmon said:
Wot, got toasted over on comp.lang.c? Unfortunately, what you are
asking is outside the standardized boundaries of either language. The
following has been known to work on various platforms and is IMO as
portable as you can get on this.

To be honest, I wasn't subscribed to any of the C newgroups but I was
subscribed to this one and figured the question wasn't totally off-topic. I
probably should have taken the time to post the question to the most
appropriate group(s). I apologize.

It's strange that the ANSI standard allows for redirecting a default stream,
but it doesn't specify a manner to reset it. That seems like an oversight.

Mike
 
J

Jack Klein

Okay, this is really a C question, not a C++ one, but cut me a little slack
please. :)

Anyway, considering one redirects a standard stream with, say, the following
code:

FILE *fp = freopen( "test.txt", "w", stdout );
puts( "This is a test." );

How would one reset stdout such that it writes data to the console?

TIA,
Mike

The C standard, from which C++ inherits these functions, does not
define a way to do this, nor guarantee that it is possible on all
platforms.

If your platform provides a name, representable as a character string,
then it is quite likely that another call to freopen() can do the
trick. Such names might be "con" or "/dev/tty" or some such, all very
platform specific.

The real problem is that based on some early implementations, and
perhaps on some still current and/or in use, not only the contents of
the structure pointed to by the FILE *, but also the actual address
may be significant.

This is covered in the comp.lang.c FAQ, BTW, link in my signature.
 

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

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top