In C, we can find the size of file using
if (fseek(fp, 0L, SEEK_END) != 0)
// Handle repositioning error
126.96.36.199(paragraph 3) of the C Standard ISO/IEC 9899:1999 specifies the following behavior for fseek() when opening a binary file in binary mode:
A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END.
In addition, footnote 267 of subclause 7.19.3 says:
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.
Seeking to the end of a
binary stream in binary mode with
fseek() is not meaningfully supported and is not a recommended method for computing the size of a file.
This recommended solution uses the size provided by the POSIX
fstat() function, rather than by
fseek(), to obtain the size of the file.