Connecting Tech Pros Worldwide Forums | Help | Site Map

closing stdout

Paul Edwards
Guest
 
Posts: n/a
#1: Oct 20 '06
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.



Cong Wang
Guest
 
Posts: n/a
#2: Oct 20 '06

re: closing stdout




On Oct 20, 6:38Â*pm, "Paul Edwards" <kerra...@nosppaam.w3.towrote:
Quote:
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.

Richard Bos
Guest
 
Posts: n/a
#3: Oct 20 '06

re: closing stdout


"Paul Edwards" <kerravon@nosppaam.w3.towrote:
Quote:
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
Hallvard B Furuseth
Guest
 
Posts: n/a
#4: Oct 20 '06

re: closing stdout


Richard Bos writes:
Quote:
>Paul Edwards wrote:
Quote:
>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
Bill Pursell
Guest
 
Posts: n/a
#5: Oct 20 '06

re: closing stdout



Cong Wang wrote:
Quote:
On Oct 20, 6:38 pm, "Paul Edwards" <kerra...@nosppaam.w3.towrote:
Quote:
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 notbe
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

Paul Edwards
Guest
 
Posts: n/a
#6: Oct 21 '06

re: closing stdout


"Richard Bos" <rlb@hoekstra-uitgeverij.nlwrote in message news:4538ade9.244955277@news.xs4all.nl...
Quote:
"Paul Edwards" <kerravon@nosppaam.w3.towrote:
>
Quote:
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.


Keith Thompson
Guest
 
Posts: n/a
#7: Oct 21 '06

re: closing stdout


"Paul Edwards" <kerravon@nosppaam.w3.towrites:
Quote:
"Richard Bos" <rlb@hoekstra-uitgeverij.nlwrote in message
news:4538ade9.244955277@news.xs4all.nl...
Quote:
>"Paul Edwards" <kerravon@nosppaam.w3.towrote:
>>
Quote:
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) kst-u@mib.org <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.
CBFalconer
Guest
 
Posts: n/a
#8: Oct 21 '06

re: closing stdout


Paul Edwards wrote:
Quote:
>
.... snip ...
Quote:
>
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>


Eric Sosman
Guest
 
Posts: n/a
#9: Oct 21 '06

re: closing stdout


CBFalconer wrote:
Quote:
Paul Edwards wrote:
>
... snip ...
>
Quote:
>>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.
http://dialspace.dial.pipex.com/town.../bloopers.html

--
Eric Sosman
esosman@acm-dot-org.invalid
Closed Thread