Question about freopen() failure

Discussion in 'C Programming' started by Kenneth Brody, Mar 21, 2008.

  1. What happens to the existing file stream if freopen() fails? For
    example:

    FILE *f = freopen("/a/filename/that/fails/to/open","w",stdout);

    If the specified filename fails to open, is stdout now closed? From
    my reading, it appears so:

    7.19.5.4p4:

    The freopen function first attempts to close any file that is
    associated with the specified stream. Failure to close the file
    is ignored.

    In other words, the current stream is closed, and only then does it
    attempt to open the specified file.

    Am I correct that an freopen() on stdout that fails leaves the program
    without any stdout, and that a future fopen() could end up giving that
    file to stdout?

    (A test program on a particular platform confirms that, on that
    platform at least, this does occur.)

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Mar 21, 2008
    #1
    1. Advertising

  2. Kenneth Brody

    Eric Sosman Guest

    Kenneth Brody wrote:
    > What happens to the existing file stream if freopen() fails? For
    > example:
    >
    > FILE *f = freopen("/a/filename/that/fails/to/open","w",stdout);
    >
    > If the specified filename fails to open, is stdout now closed? From
    > my reading, it appears so:
    >
    > 7.19.5.4p4:
    >
    > The freopen function first attempts to close any file that is
    > associated with the specified stream. Failure to close the file
    > is ignored.


    It seems to me that stdout "is closed" after the failed
    freopen(), but that the original stream may or may not have
    been "properly closed" -- for example, it may or may not
    have been fflush()ed. It would certainly be a bad idea to
    try to use stdout after the failure ...

    You might want to try this question on comp.std.c.

    --
    Eric Sosman, Mar 21, 2008
    #2
    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,186
    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,333
    vidyaani
    Jan 11, 2013
  3. Teh Charleh

    freopen()

    Teh Charleh, Dec 7, 2003, in forum: C Programming
    Replies:
    1
    Views:
    480
    Enrico `Trippo' Porreca
    Dec 7, 2003
  4. John Devereux

    freopen()

    John Devereux, Sep 27, 2005, in forum: C Programming
    Replies:
    3
    Views:
    496
    John Devereux
    Sep 27, 2005
  5. tmpfile and freopen

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

Share This Page