The C spec has an interesting footnote (#268 C11dr §7.21.3 9)
"Setting the file position indicator to end-of-file, as with
fseek(file, 0, SEEK_END)
, has undefined behavior for a binary stream (because of possible trailing null characters) or for any stream with state-dependent encoding that does not assuredly end in the initial shift state."
Does this ever apply to binary streams reading a file? (as from a physical device)
IMO, a binary file on a disk is just a sea of bytes. It seems to me that a binary file could not have state-dependent encoding as it is a binary file. I'm fuzzy on the concept of "binary wide-oriented streams" and if that even could apply to disk I/O.
I see that calling fseek(file, 0, SEEK_END)
on a serial stream like a com port or maybe stdin
may not get to the true end as the end is yet to be determined. Thus the narrowing of the question to physical files.
[edit] Answer: A concern with older (maybe up to late 1980s). Presently in 2014, Windows, POSIT-specific and non-exotic others: not a problem.
@Shafik Yaghmour provides a good reference in Using fseek and ftell to determine the size of a file has a vulnerability?. There @Jerry Coffin discusses CP/M as binary files not always having a precise length. (128-byte records per wiki).
Thanks to @Keith Thompson answer for the meat of the answer.
Together this explains the specs's "(because of possible trailing null characters)" comment.