closing stdout

Discussion in 'C Programming' started by Paul Edwards, Oct 20, 2006.

  1. Paul Edwards

    Paul Edwards Guest

    Is it permissible in C89 to do an "fclose(stdout)"
    and then exit, or is the C runtime library allowed to
    assume that stdout remains open and thus unconditionally
    do an fclose itself?

    Thanks. Paul.
    Paul Edwards, Oct 20, 2006
    #1
    1. Advertising

  2. Paul Edwards

    Cong Wang Guest

    On Oct 20, 6:38 pm, "Paul Edwards" <3.to> wrote:
    > Is it permissible in C89 to do an "fclose(stdout)"
    > and then exit, or is the C runtime library allowed to
    > assume that stdout remains open and thus unconditionally
    > do an fclose itself?
    >
    > Thanks.  Paul.


    In C99, you can do that. But I am not familar with C89, I can't promise
    C89 also allows you to do that. I don't know why you are care for C89.
    In fact, you can find the following rules in C99:
    (1)
    At program startup, three text streams are predeï¬ned and need not be
    opened explicitly— standard input (for reading conventional input),
    standard output (for writing conventional output), and standard error
    (for writing diagnostic output).
    (2)
    If the main function returns to its original caller, or if the exit
    function is called, all open ï¬les are closed (hence all output
    streams are flushed) before program termination.
    Cong Wang, Oct 20, 2006
    #2
    1. Advertising

  3. Paul Edwards

    Richard Bos Guest

    "Paul Edwards" <3.to> wrote:

    > Is it permissible in C89 to do an "fclose(stdout)"
    > and then exit, or is the C runtime library allowed to
    > assume that stdout remains open and thus unconditionally
    > do an fclose itself?


    Yes, it's allowed. Whether it's wise is another matter, since in C89
    there is no portable way to get it back, and in C99 there is one way
    which is portable, but not guaranteed to work.

    Richard
    Richard Bos, Oct 20, 2006
    #3
  4. Richard Bos writes:
    >Paul Edwards wrote:
    >> Is it permissible in C89 to do an "fclose(stdout)"
    >> and then exit, (...)

    >
    > Yes, it's allowed. Whether it's wise is another matter, since in C89
    > there is no portable way to get it back,


    Why would he want to get it back? He's exit()ing.
    It's wise to close it if you want to exit with a failure if
    fclose() failed, since exit() doesn't do that by itself.

    --
    Hallvard
    Hallvard B Furuseth, Oct 20, 2006
    #4
  5. Paul Edwards

    Bill Pursell Guest

    Cong Wang wrote:
    > On Oct 20, 6:38 pm, "Paul Edwards" <3.to> wrote:
    > > Is it permissible in C89 to do an "fclose(stdout)"
    > > and then exit, or is the C runtime library allowed to
    > > assume that stdout remains open and thus unconditionally
    > > do an fclose itself?
    > >
    > > Thanks. Paul.

    >
    > In C99, you can do that. But I am not familar with C89, I can't promise
    > C89 also allows you to do that. I don't know why you are care for C89.
    > In fact, you can find the following rules in C99:
    > (1)
    > At program startup, three text streams are predeï¬ned and need not be
    > opened explicitly— standard input (for reading conventional input),
    > standard output (for writing conventional output), and standard error
    > (for writing diagnostic output).


    But, at a bash prompt, I can type:
    $ my_prog <&-
    in which case the program starts up with stdin closed. Does this
    mean that bash is a non-conforming environment?

    --
    Bill Pursell
    Bill Pursell, Oct 20, 2006
    #5
  6. Paul Edwards

    Paul Edwards Guest

    "Richard Bos" <> wrote in message news:4all.nl...
    > "Paul Edwards" <3.to> wrote:
    >
    > > Is it permissible in C89 to do an "fclose(stdout)"
    > > and then exit, or is the C runtime library allowed to
    > > assume that stdout remains open and thus unconditionally
    > > do an fclose itself?

    >
    > Yes, it's allowed. Whether it's wise is another matter, since in C89
    > there is no portable way to get it back, and in C99 there is one way
    > which is portable, but not guaranteed to work.


    Thankyou. Now is the application program allowed to
    call fclose(stdout) twice, and if so, what is the expected
    return on the second close?

    BFN. Paul.
    Paul Edwards, Oct 21, 2006
    #6
  7. "Paul Edwards" <3.to> writes:
    > "Richard Bos" <> wrote in message
    > news:4all.nl...
    >> "Paul Edwards" <3.to> wrote:
    >>
    >> > Is it permissible in C89 to do an "fclose(stdout)"
    >> > and then exit, or is the C runtime library allowed to
    >> > assume that stdout remains open and thus unconditionally
    >> > do an fclose itself?

    >>
    >> Yes, it's allowed. Whether it's wise is another matter, since in C89
    >> there is no portable way to get it back, and in C99 there is one way
    >> which is portable, but not guaranteed to work.

    >
    > Thankyou. Now is the application program allowed to
    > call fclose(stdout) twice, and if so, what is the expected
    > return on the second close?


    After a call to fclose(stdout), the value of stdout is indeterminate.
    Calling fclose(stdout) a second time invokes undefined behavior.

    If you call fclose(stdout) once, then it won't be closed again when
    the program terminates. C99 7.20.4.3p4 says:

    Next, all open streams with unwritten buffered data are flushed,
    all open streams are closed, and all files created by the tmpfile
    function are removed.

    If stdout has been closed, then it isn't an open stream.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 21, 2006
    #7
  8. Paul Edwards

    CBFalconer Guest

    Paul Edwards wrote:
    >

    .... snip ...
    >
    > Thankyou. Now is the application program allowed to
    > call fclose(stdout) twice, and if so, what is the expected
    > return on the second close?


    Boom.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Oct 21, 2006
    #8
  9. Paul Edwards

    Eric Sosman Guest

    Eric Sosman, Oct 21, 2006
    #9
    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. Martijn Brouwer

    closing stdin, stdout and stderr

    Martijn Brouwer, Dec 26, 2005, in forum: Python
    Replies:
    5
    Views:
    1,105
    Donn Cave
    Dec 27, 2005
  2. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    650
    Thomas Hansen
    Feb 15, 2007
  3. Matt Bleh
    Replies:
    6
    Views:
    155
    David Masover
    Sep 22, 2009
  4. thomas
    Replies:
    0
    Views:
    226
    thomas
    Oct 23, 2003
  5. Noel Sant

    Closing and re-opening redirected STDOUT

    Noel Sant, Feb 25, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    124
    Anno Siegel
    Feb 25, 2004
Loading...

Share This Page