freopen()

Discussion in 'C Programming' started by John Devereux, Sep 27, 2005.

  1. Hi,

    I am a bit confused about the behaviour of freopen() and the standard
    i/o streams.

    I am trying to redirect a stream (stderr) to a particular "file". But,
    if the call fails I want it to stay as it was, or at least be able to
    redirect it somewhere else (e.g. stdout).

    For example I would like to do,

    ..
    stderr = freopen("/dev/errors","w", stderr); /* may fail */
    if(!stderr) freopen("/dev/screen", "w", stderr);


    --

    John Devereux
    John Devereux, Sep 27, 2005
    #1
    1. Advertising

  2. In article <>,
    John Devereux <> wrote:

    >I am trying to redirect a stream (stderr) to a particular "file". But,
    >if the call fails I want it to stay as it was, or at least be able to
    >redirect it somewhere else (e.g. stdout).


    According to the standard, "The freopen function first attempts to
    close any file that is associated with the specified stream". So if
    opening the new file fails, it's too late to revert to the old one.

    (You may be able to do this using something operating-system specific
    instead, such as dup2() in unix.)

    I see nothing that implies that you can't attempt to freopen() the
    same stream again. (This may explain the "... any file ..." wording.)

    > stderr = freopen("/dev/errors","w", stderr); /* may fail */


    Don't do this. You can't assign to stderr. freopen() changes the file
    associated with the stream rather than creating a new stream. Just
    do

    if(!freopen("/dev/errors","w", stderr))
    ...

    -- Richard
    Richard Tobin, Sep 27, 2005
    #2
    1. Advertising

  3. (Richard Tobin) writes:

    > In article <>,
    > John Devereux <> wrote:
    >
    > >I am trying to redirect a stream (stderr) to a particular "file". But,
    > >if the call fails I want it to stay as it was, or at least be able to
    > >redirect it somewhere else (e.g. stdout).

    >
    > According to the standard, "The freopen function first attempts to
    > close any file that is associated with the specified stream". So if
    > opening the new file fails, it's too late to revert to the old one.
    >
    > (You may be able to do this using something operating-system specific
    > instead, such as dup2() in unix.)
    >
    > I see nothing that implies that you can't attempt to freopen() the
    > same stream again. (This may explain the "... any file ..."
    > wording.)



    > > stderr = freopen("/dev/errors","w", stderr); /* may fail */

    >
    > Don't do this. You can't assign to stderr. freopen() changes the file
    > associated with the stream rather than creating a new stream. Just
    > do
    >
    > if(!freopen("/dev/errors","w", stderr))
    > ...


    Just to clarify, do you mean I can freopen() stderr with another
    stream, even after the failure?

    if(!freopen("/dev/errors","w", stderr))
    {
    stderr = freopen("/dev/screen","w", stderr);
    }


    I gather freopen() returns a null pointer if unsuccessful, does that
    then mean (stderr==0) ? I read that sometimes stderr is implemented as
    a macro e.g.

    #define stderr (&iob[2])

    But if so how can the call

    stderr = freopen(..)

    be legal?


    Thanks,

    --

    John Devereux
    John Devereux, Sep 27, 2005
    #3
  4. John Devereux <> writes:


    <SNIP>
    >
    > But if so how can the call
    >
    > stderr = freopen(..)
    >
    > be legal?


    Sorry, I now see it's not legal. I was confused because my
    implementation did not complain.


    --

    John Devereux
    John Devereux, Sep 27, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur

    freopen & cout

    Alex Vinokur, Dec 2, 2003, in forum: C++
    Replies:
    1
    Views:
    1,182
    Jack Klein
    Dec 3, 2003
  2. Alex Vinokur

    freopen, fclose and stdout

    Alex Vinokur, Dec 3, 2003, in forum: C Programming
    Replies:
    4
    Views:
    8,317
    vidyaani
    Jan 11, 2013
  3. Teh Charleh

    freopen()

    Teh Charleh, Dec 7, 2003, in forum: C Programming
    Replies:
    1
    Views:
    475
    Enrico `Trippo' Porreca
    Dec 7, 2003
  4. tmpfile and freopen

    , Jun 5, 2006, in forum: C Programming
    Replies:
    1
    Views:
    352
    Barry Schwarz
    Jun 15, 2006
  5. freopen

    , Aug 18, 2007, in forum: C Programming
    Replies:
    4
    Views:
    739
Loading...

Share This Page