closing stdout

P

Paul Edwards

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

Cong Wang

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

Richard Bos

Paul Edwards said:
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
 
H

Hallvard B Furuseth

Richard said:
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.
 
B

Bill Pursell

Cong said:
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?
 
P

Paul Edwards

Richard Bos said:
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.
 
K

Keith Thompson

Paul Edwards said:
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.
 
C

CBFalconer

Paul said:
.... 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Working with files 1
Temporarily close stdout? 5
hexump.c 79
redirect stdout 10
Binary mode for stdout? 5
URGENT 1
Closing an overlay outside the overlay as well 1
Taking list as an input from Python to C 1

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top