First, proper understanding of the difference between text mode and binary mode is important when using functions that operate on file streams (see FIO14-C. Understand the difference between text mode and binary mode with file streams).
Section 7.19.9.2 of C99 ISO/IEC 9899:1999 states the following specific behavior for fseek() when opening a binary file in binary mode: ISO/IEC 9899:1999
A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END.
In addition, footnote 234 of Section 7.19.3 of C99 has this to say: ISO/IEC 9899:1999 has this to say:
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.
These statements indicate that setting the file position indicator to end-of-file of a binary stream opened in binary mode with fseek() is not meaningfully supported, thereby resulting in undefined behavior and should thus be avoided.
Section 7.19.9.4 of C99 ISO/IEC 9899:1999 states the following specific behavior for ftell() when opening a text file in text mode: ISO/IEC 9899: 1999
For a text stream, its file position indicator contains unspecified information, usable by the fseek function for returning the file position indicator for the stream to its position at the time of the ftell call.
...
However, setting the file position indicator to the end of the file with fseek() has undefined behavior for a binary stream and thus consequently the amount of memory allocated may be incorrect, leading to a potential vulnerability.
...
However, the file position indicator returned by ftell() with a file opened in text mode is only useful in calls to fseek(). As such, the value of file_size may not necessarily be a meaningful measure of the number of characters in the file and thus consequently the amount of memory allocated may be incorrect, leading to a potential vulnerability.
...
Implementation Details
MSDN MSDN has the following to say about ftell(): MSDN
The value returned by ftell may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return-linefeed translation. Use ftell with fseek to return to file locations correctly.
...