The fseek and ftell functions are both defined by the ISO C language standard.
Below is the latest public draft of the 2011 C standard, but the ISO C standards of 1990, 1999 and 2011 are very similar in this area, if not identical.
7.21.9.4:
The ftell function gets the current value of the file position of the stream indicator pointed to by the stream . For a binary stream, the value is the number of characters at the beginning of the file. For a text stream, the file position indicator contains unspecified information used by the fseek function to return the file position indicator for the stream to its position during the ftell call; the difference between two such return values ββis not necessarily a significant measure of the number of characters written or read.
7.21.9.2:
The fseek function sets the file position indicator for the stream pointed to by the stream . If a read or write error occurs, the error indicator for the stream is set and fseek does not work.
For a binary stream, the new position, measured in characters from the beginning of the file, is obtained by adding offset to the position indicated from where . The indicated position is the beginning of the file, if SEEK_SET is coming from , the current value of the file position indicator, if SEEK_CUR , or the end of the file, if SEEK_END . The binary stream does not require significant support for fseek calls with a value of SEEK_END .
For a text stream, either offset must be zero, or offset must be the value returned by a successful ftell call previously for the stream associated with the same file, and where should SEEK_SET come from .
Violation of any of the provisions "must" makes the behavior of your program undefined.
So, if the file was opened in binary mode, ftell gives you the number of characters at the beginning of the file, but fseek relative to the end of the file ( SEEK_END ) is not necessarily meaningful. This allows you to use systems that store binary files in entire blocks and do not track how much was written to the last block.
If the file was opened in text mode, you can search for the beginning or end of the file with offset 0 or you can search for the position specified by the previous ftell call; fseek with any other arguments has undefined behavior. It hosts systems in which the number of characters read from a text file does not necessarily correspond to the number of bytes in the file. For example, when reading Windows, the CR-LF pair ( "\r\n" ) reads only one character, but advances 2 bytes in the file.
In practice, on Unix-like systems, text and binary modes behave the same, and the fseek / ftell method will work. I suspect it will work on Windows (I think ftell will give a byte offset that may not be the same as the number of times you could call getchar() in text mode).
Note that ftell() returns a result of type long . On systems where long is 32 bits, this method cannot work for files of 2 gigabytes or more.
It may be useful for you to use some kind of system method to get the file size. Since the fseek / ftell method is in any case a system method, for example stat() on Unix-like systems.
On the other hand, fseek and ftell can work, as you expect, on most systems that you are likely to encounter. I am sure that there are systems in which this will not work; Sorry, but I have no features.
If working with Linux and Windows is good enough and you are not dealing with large files, then the fseek / ftell method is probably appropriate. Otherwise, you should use the system method to determine the file size.
And keep in mind that everything that tells you the file size can only tell you the size of this moment. File size may change before access.