Jordan said:
Nor is it necessary. fread does not need to be implemented in pure
standard C. To suppose that it's "getc in a loop" is naive.
My naiveté may be the stuff of legend, but even I am
not *that* naive. Nonetheless, I find it hard to imagine
accepting data from a transient and un-replayable source
while retaining the ability to say "Woops! I went too far
and must make all this data available via a subsequent
getc()" without buffering the data *somewhere*. My point
is about the difficulty of providing such a buffer, of
potentially large size, and for potentially every input or
update stream.
Or allocate a buffer on the stack within fread. It's unlikely to
actually be too large, given that the user also has to have
allocated a whole array of items in the given size.
"On the stack within fread" won't suffice. If the
"overrun" characters are to be accessible to a subsequent
getc(), their buffer must persist until getc() empties it.
If the buffer were to vanish when fread() returned, how
would getc() find it? (Again, I'm not supposing that these
functions need be implemented in C; the persistence of the
buffer, not its location, is the troublesome point.)
Well, with a closer reading of the standard i'll concede that it's
not the case when using a stream that isn't seekable, but
The file position indicator for the stream (if defined) is advanced
by the number of characters successfully read. /* i'm assuming that
characters which are part of a partial item are not "successfully
read". */
The Standard is not clear about what constitutes "success"
here. Another reading is that fread() can "successfully" read
less than an item's worth of bytes, and this is the interpretation
P.J. Plauger uses in "The Standard C Library." PJP is officially
non-normative, of course, but he is widely considered to be
authoritatively non-normative ;-) That is, his interpretation
is likely to be a pretty good hint about the Committee's intent,
even if the Standard's expression of that intent has the ability
to confuse the rest of us.