473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

redirect stdout

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
10 21104
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
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

"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
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

"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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: newgene | last post by:
Hi, group, I guess it is a simple question for you, but it puzzles me for a while. I use a wxTextCtrl in my application for output result and any exception msg. I know I can add new msg onto...
6
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* ....
0
by: Neal Becker | last post by:
I'd like to build a module that would redirect stdout to send it to a logging module. I want to be able to use a python module that expects to print results using "print" or "sys.stdout.write()"...
4
by: Paul Nilsson | last post by:
Hi, Does anyone know how to redirect text output when issuing a system or an execlp call? I have a GUI that insists on opening up a console whenever I give a system function call, and I would...
6
by: Mazze | last post by:
Hi. I have a strange problem ... I want to create a process and capture stdout : ... gint std_out; char* argv = {"/opt/sun-jdk-1.4.2.08/bin/java","helloworld",NULL}; //char* argv =...
3
by: mikem76 | last post by:
How do I automatically redirect stdout and stderr when using os.popen2 to start a long running process. If the process prints a lot of stuff to stdout it will eventually stop because it runs out...
0
by: danebarney | last post by:
I'm developing a Windows CE 5.0 app using C# and I need to call a child process and then capture its stdout into a window. The solutions I've read online for doing this involve either using the...
10
by: Jef Driesen | last post by:
I wrote a program that writes a large amount of information to stdout (and stderr). When run from the commandline, this output either appears on the console window (the default) or can be...
0
by: Neal Becker | last post by:
Is there a simple way to use a 'with' statement to redirect stdout in a block?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.