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

redirect stdout

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Michael Gaab wrote:
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

Nov 13 '05 #2

P: n/a
In article <vr************@corp.supernews.com>,
Noah Roberts <nr******@dontemailme.com> wrote:
Michael Gaab wrote:
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...


[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

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I thought everyone here was insane. That's why I come to visit, so
I'll feel at home.
--Mike Wahler in comp.lang.c
Nov 13 '05 #3

P: n/a

"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:vr************@corp.supernews.com...
Michael Gaab wrote: Hmmm, I don't know if this would work but...

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

Nov 13 '05 #4

P: n/a
Michael Gaab <mi****@montana.com> scribbled the following:
"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:vr************@corp.supernews.com...
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.


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla
Nov 13 '05 #5

P: n/a

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bo**********@oravannahka.helsinki.fi...
Michael Gaab <mi****@montana.com> scribbled the following:
"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:vr************@corp.supernews.com...
Michael Gaab wrote:
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.


Nov 13 '05 #6

P: n/a
Greetings.

In article <3f********@127.0.0.1>, Michael Gaab wrote:
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

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #7

P: n/a
Michael Gaab wrote:

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 is Question 12.34 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun.com
Nov 13 '05 #8

P: n/a
Michael Gaab wrote:
"Noah Roberts" <nr******@dontemailme.com> wrote in message
news:vr************@corp.supernews.com...
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.


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

Nov 13 '05 #9

P: n/a
"Michael Gaab" <mi****@montana.com> wrote:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote: <snip>
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.


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
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #10

P: n/a
In <vr************@corp.supernews.com> Noah Roberts <nr******@dontemailme.com> writes:
Michael Gaab wrote:
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;


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.