Geoff Clare said:
>> More precisely, if a program is required to read standard input or write
>> standard output in binary mode, how does the programmer do this, since thos
>> streams will initially have opened in text mode.
> Yes, of course! I hadn't thought about changing between text and binary
> modes on non-UNIX systems. (Concentrating too much on UNIX specifics.)
Indeed.
> Unfortunately I think the current requirements in C99 don't quite
> provide the intended capability. Sure you can change stdin and stdout
> to binary mode, but I can't see any way to implement cat properly using
> freopen(NULL, ...), given that the "as if" requirement means that
> changing to mode "rb" rewinds the stream and changing to mode "wb"
> truncates the file.
Hmm; you're thinking of complex Unix pipelines, I take it.
> It would work fine in the (admittedly common) case where the file
> pointer for stdin is initially at the beginning of the file, and
> the file open on stdout is empty. I can't see how to make it work
> for the other cases.
You know, I can make a reasonable argument that at program startup it is
required for stdin/stdout/stderr to act as if they have just been opened:
[#7] At program startup, three text streams are predefined
and need not be opened explicitly
implies that they have been opened as if by stdin. So the file open on
stdin must be at the start and the ones on stdout/stderr must either be at
the start or opened in an append mode.
> Why on earth did the C99 folks put the "as if" clause in there in the
> first place? It seems to have caused nothing but problems.
Because we didn't forsee the problems.
> Surely it
> would have been much more useful to specify that freopen(NULL, ...)
> changes the mode *without* the side effects that an freopen() with the
> filename would have had, not with them.
The mental model we had was that:
freopen (NULL, mode, stream)
is shorthand for:
freopen (__getfilename (stream), mode, stream)
which is how people used to have to do this (and couldn't do it if they
didn't have a record of the filename). Having seen the issues, I agree
that it doesn't suffice.
--
Clive D.W. Feather | Work: <yyyyy@xxxxxxxxx> | Tel: +44 20 8495 6138
Internet Expert | Home: <yyyyy@xxxxxxxxxx> | *** NOTE CHANGE ***
Demon Internet | WWW: http://www.davros.org | Fax: +44 870 051 9937
Thus plc | | Mobile: +44 7973 377646
|