redirect stdout

M

Michael Gaab

If I redirect stdout by using freopen("afile", "w", stdout);
and then I closed stdout using, fclose(stdout), essentially I am just
closing "afile".

I have to reestablish what stdout originally pointed to?

thanks
 
M

Michael Gaab

Noah Roberts said:
Michael Gaab wrote:
Hmmm, I don't know if this would work but...

FILE *save_stdout = stdout;
stdout = fopen("xyz", "w");
.
close(stdout);
stdout = save_stdout;

I already tried something similar to that and the compiler complained that
stdout is const.
But that is a little confusing because if stdout is const then how can
stdout be redirected?
thanks.
 
M

Michael Gaab

It can be redirected because in redirection, it goes as the parameter
to the freopen() function, and C function parameters can be any values,
const or not const. It's assingment that causes troubles with const
variables.

Ok. I understand that.

But if you look at my original post then I don't need to close stdout but
rather "afile" using fclose. Since stdout is const, it will never change so
it is not necessary to reestablish it, as my intuition suggests.

thanks.
 
T

Tristan Miller

Greetings.

If I redirect stdout by using freopen("afile", "w", stdout);
and then I closed stdout using, fclose(stdout), essentially I am just
closing "afile".

I have to reestablish what stdout originally pointed to?

This question is addressed by §12.34 of the FAQ (http://www.eskimo.com
~scs/C-faq/q12.34.html), quoted here for your convenience:

Once I've used freopen, how can I get the original stdout (or stdin)
back?

There isn't a good way. If you need to switch back, the best solution is
not to have used freopen in the first place. Try using your own
explicit output (or input) stream variable, which you can reassign at
will, while leaving the original stdout (or stdin) undisturbed.

Regards,
Tristan
 
N

Noah Roberts

Michael said:
If I redirect stdout by using freopen("afile", "w", stdout);
and then I closed stdout using, fclose(stdout), essentially I am just
closing "afile".

I have to reestablish what stdout originally pointed to?

Hmmm, I don't know if this would work but...

FILE *save_stdout = stdout;
stdout = fopen("xyz", "w");
..
..
..
close(stdout);
stdout = save_stdout;

NR
 
D

Dave Vandervies

Hmmm, I don't know if this would work but...

[making a backup copy of the stdout pointer and reassigning it]

Sometimes, but not portably, if I'm not mistaken; stdout needs to
expand to an expression of type FILE *, but that expression need not
be a modifiable lvalue, and I seem to recall reports of sightings of
current implementations where it isn't one last time this came up.


dave
 
J

Joona I Palaste

I already tried something similar to that and the compiler complained that
stdout is const.
But that is a little confusing because if stdout is const then how can
stdout be redirected?
thanks.

It can be redirected because in redirection, it goes as the parameter
to the freopen() function, and C function parameters can be any values,
const or not const. It's assingment that causes troubles with const
variables.
 
N

Noah Roberts

Michael said:
I already tried something similar to that and the compiler complained that
stdout is const.

I would warrant a guess then that there is no portable answer. In UNIX
you close fd 1 and then open() (not fopen) a new file in such a way as
to make it that fd. Then stdout is automatically redirected because it
is just a wrapper around the fd (basically).
But that is a little confusing because if stdout is const then how can
stdout be redirected?

freopen must be a wrapper around the system specific things that must be
done like the above. I have never used it...

NR
 
I

Irrwahn Grausewitz

Michael Gaab said:
"Joona I Palaste" <[email protected]> wrote:

Ok. I understand that.

But if you look at my original post then I don't need to close stdout but
rather "afile" using fclose.

But looking at your OP the only FILE* to "afile" /is/ stdout, the only
ways to close it are calling fclose(stdout) or exit().
Since stdout is const, it will never change so
it is not necessary to reestablish it, as my intuition suggests.

Err, not quite: If freopen() succeeds (!), the characteristics of the
stream designated by stdout are changed, though stdout /itself/ might
still be non-modifiable.

What about something like this:

FILE *my_stdout;

my_stdout = fopen( "afile", ... )
/* work on my_stdout (-> afile) */
fclose( my_stdout );

my_stdout = stdout;
/* work on my_stdout (-> stdout) */

HTH
Regards
 
D

Dan Pop

In said:
Hmmm, I don't know if this would work but...

FILE *save_stdout = stdout;
stdout = fopen("xyz", "w");
.
.
.
close(stdout);
stdout = save_stdout;

Nonsense. It's not the value of the FILE pointer that determines the
status of the stream. The whole sequence above does nothing to alter
the value of stdout, it is the value of the object pointed to by stdout
that is being altered. And it cannot be restored by copying it to another
FILE object and back to the original, because this value is also connected
with the state of other entities, like the associated buffer and the
associated file.

Dan
 

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,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top