473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 5888


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 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.

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 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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* ....
4
by: flupke | last post by:
Hi, I have a gui (made in wxPython) that enables a user to connect to a server and issue some commands. The problem occurs when i try to disconnect the client. It exits but it doesn't return to...
4
by: Leon | last post by:
"stdout" is file object, it open mode is "w" and it have a close() fuction..... while it run close(), how to reopen it because I want to do rewrite it stdout content update
7
by: Mike | last post by:
I would like my 'print' statements to send its output to the user's screen and a log file. This is my initial attempt: class StdoutLog(file): def __init__(self, stdout,...
5
by: Martijn Brouwer | last post by:
I am writing a unix daemon in python, so I want to close stdin, stdout and stderr. My first attempt was to the standard file descriptors using their close() methods. After closing stdout, I could...
9
by: Santtu Nyrhinen | last post by:
Hi, Let say that I have a function like void writeHello() { printf("Hello"); } Now I need to make an automated test fot that function. The test function returns 1 for successful and 0 for...
13
by: Vincent Delporte | last post by:
Hi I'm a Python newbie, and would like to rewrite this Perl scrip to be run with the Asterisk PBX: http://www.voip-info.org/wiki/view/Asterisk+NetCID Anyone knows if those lines are...
5
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post by:
I wanna print the log to both the screen and file, so I simulatered a 'tee' class Tee(file): def __init__(self, name, mode): file.__init__(self, name, mode) self.stdout = sys.stdout...
5
by: Joakim Hove | last post by:
Hello, I have written a program in C; this programs uses an external proprietary library. When calling a certain function in the external library, the particular function writes a message to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.