comp.lang.c
[Top] [All Lists]

Re: Temporarily close stdout?

Subject: Re: Temporarily close stdout?
From: Richard Tobin
Date: 30 Apr 2008 13:07:05 GMT
Newsgroups: comp.lang.c

In article <64f07b4c-2742-45a0-bbc3-38c9f2c67c29@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Joakim Hove  <joakim.hove@xxxxxxxxx> wrote:

>   /* Temporarily close stdout: */
>   fclose(stdout);
>   job_nr = lsb_submit( &request , &reply );
>   /* Reopen stdout */
>   stdout = fdopen(1 , "a");

I don't think there's any standard C way to to what you want.

It looks as if you're using something Posix-like.  The fclose(stdout)
will have closed the underlying file descriptor (1), so your fdopen()
doesn't work.  As someone else said, you could use dup() to keep a
copy of it.  But standard C doesn't let you assign to stdout; I'm not
sure whether Posix does.  I have a vague recollection of some systems
providing fdreopen(), but it doesn't seem to be standard.  You could
fdopen() a new FILE *, and use that instead of stdout in your code,
or if you have /dev/fd you could use freopen() on /dev/fd/N, where N
is what you got back from dup().

But closing stdout is dubious in the first place.  Are you sure the
library won't complain about stdout being closed?  It would be better
to freopen() stdout to /dev/null.

-- Richard
-- 
:wq

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy