468,512 Members | 1,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

closing stdout

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.
Oct 20 '06 #1
8 4900


On Oct 20, 6:38Â*pm, "Paul Edwards" <kerra...@nosppaam.w3.towrote:
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 predefined 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 files are closed (hence all output
streams are flushed) before program termination.

Oct 20 '06 #2
"Paul Edwards" <ke******@nosppaam.w3.towrote:
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
Oct 20 '06 #3
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
Oct 20 '06 #4

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

Oct 20 '06 #5
"Richard Bos" <rl*@hoekstra-uitgeverij.nlwrote in message news:45****************@news.xs4all.nl...
"Paul Edwards" <ke******@nosppaam.w3.towrote:
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.
Oct 20 '06 #6
"Paul Edwards" <ke******@nosppaam.w3.towrites:
"Richard Bos" <rl*@hoekstra-uitgeverij.nlwrote in message
news:45****************@news.xs4all.nl...
>"Paul Edwards" <ke******@nosppaam.w3.towrote:
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) ks***@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.
Oct 21 '06 #7
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>
Oct 21 '06 #8
CBFalconer wrote:
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.
http://dialspace.dial.pipex.com/town.../bloopers.html

--
Eric Sosman
es*****@acm-dot-org.invalid
Oct 21 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tsai Li Ming | last post: by
4 posts views Thread by flupke | last post: by
4 posts views Thread by Leon | last post: by
7 posts views Thread by Mike | last post: by
5 posts views Thread by Martijn Brouwer | last post: by
9 posts views Thread by Santtu Nyrhinen | last post: by
13 posts views Thread by Vincent Delporte | last post: by
5 posts views Thread by =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post: by
5 posts views Thread by Joakim Hove | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.