Re: fclose then fopen equivalent for stdout?



Eric Sosman wrote:
David Mathog wrote On 10/25/06 12:48,:
A program of mine writes to a tape unit. Output can be either through
stdout or through a file opened with fopen(). When all the data is
transferred to tape the program needs to close the output stream so that
the tape driver will write a filemark on the tape. Otherwise multiple
clumps of data saved to tape would all appear to be one big file on the
tape.

When the tape unit device was explicitly opened with fopen()
that's possible: call fclose() and then for the next batch
of data fopen() the tape device again and write some more.

However when data is going through stdout like:

program > /dev/nst0

is there an equivalent operation? Maybe something like this:

fputc(stdout,EOF);

(Corrected to fputc(EOF, stdout) in a follow-up.)

No, this would merely write a character to the output.
The identity of exactly which character gets written is a
little fuzzy in that it depends on the value of the EOF
macro (usually -1, but could be another negative integer)
and on what you get when converting that value to a char
in the local encoding.

or this

freopen(NULL,"wb",stdout);

Undefined behavior: The first argument is supposed to
be a string (a string that names a file), but NULL is not
a string. It's much like trying fopen(NULL, "wb").

Actually, freopen() has special behaviour for a NULL filename. I don't
know enough about it to know if that special behaviour is useful here,
though.

Here's a suggestion: You're trying to obtain the effect
of fclose(), right? Does any particular function spring to
mind as being likely to perform the operations of fclose()?
How about ... <<wait for it>> ... fclose()?

So how do you write to stdout again after closing it?

.



Relevant Pages

  • Re: fclose then fopen equivalent for stdout?
    ... transferred to tape the program needs to close the output stream so that ... the tape driver will write a filemark on the tape. ... However when data is going through stdout like: ... fclose succeeds, stdout is dead. ...
    (comp.lang.c)
  • Re: fclose then fopen equivalent for stdout?
    ... stdout or through a file opened with fopen. ... the tape driver will write a filemark on the tape. ... Also it might be worth keeping in mind that most Unix-type systems ... and if you are using stdout then you don't know the tape device ...
    (comp.lang.c)
  • Re: Mounting tape drives between systems
    ... N.B. Unsolicited email from vendors will not be appreciated. ... > tar will allow you to output to stdout. ... > rsh to dd on the remote system and thence to the tape. ...
    (AIX-L)
  • Re: fclose then fopen equivalent for stdout?
    ... transferred to tape the program needs to close the output stream so that ... the tape driver will write a filemark on the tape. ... However when data is going through stdout like: ...
    (comp.lang.c)
  • Re: fclose then fopen equivalent for stdout?
    ... transferred to tape the program needs to close the output stream so that ... the tape driver will write a filemark on the tape. ... what part of fclose exactly triggers the filemark writing? ...
    (comp.lang.c)