Re: Temporarily close stdout?



In article <42aa5189-6a17-41d9-9528-4c1361d99170@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Joakim Hove <joakim.hove@xxxxxxxxx> wrote:

/* Temporarily redirect stdout -> /dev/null */
stdout = freopen("/dev/null" , "w" , stdout);

Don't assign the result to stdout. freopen() changes stdout itself.

/* restore stdout */
stdout = freopen("/dev/stdout" , "w" , stdout);

But - the last freopen() fails with 2:No such file or directory.

Probably it closes stdout before trying to open /dev/stdout.

The dup() approach with /dev/fd would be something like (not tested):

char buf[20];
int saved_stdout = dup(1);

freopen("/dev/null", "w", stdout);
lsb_submit( &request , &reply );
sprintf(buf, "/dev/fd/%d", saved_stdout);
freopen(buf, "w", stdout);

To avoid dup(), but still relying on /dev/stdout, do

FILE *mystdout = fopen("/dev/stdout", w);
freopen("/dev/null", "w", stdout);

and change your code to write to write to mystdout instead of stdout.

You'd be wise to ask in a unix, linux or posix newsgroup, because
there may be subtleties to the workings of /dev/stdout and /dev/fd/.

-- Richard
--
:wq
.



Relevant Pages

  • Re: problems about redirect popened FILE stream to stdout
    ... In the wrong code, i pass popen "r", so can't redirect it to stdout, ... You don't need to dup at all. ... "w" as the mode to popen, the child process inherits the stdout ... Setting mode to "r" makes the child's output ...
    (comp.unix.programmer)
  • Re: Q: How to catch stdout in tcl
    ... descriptor to stdout. ... extension's [dup] command. ... set savedStdout ... Tcl or the Expect extension. ...
    (comp.lang.tcl)
  • Re: Watch contents of log-file - why doesnt it work?
    ... On Linux, it reopens the resource associated with the fd 1, so ... I never used dup for anything. ... still pointing to whatever it was pointing to at the time it ... no stdout it was just closed). ...
    (comp.unix.shell)
  • Re: Q: How to catch stdout in tcl
    ... # Save the old stdout and retarget stdout to a file ... set savedStdout [dup stdout] ...
    (comp.lang.tcl)
  • Re: How to redirect the output of eval?
    ... Consider it displays some result in stdout. ... result should be redirected to a remote machine through socket {like ... Something like [dup] of stdout, ...
    (comp.lang.tcl)