This question is in reference to How can I get a file's size in C?
Most answers suggesting the use of the function stat to get the file length also comes with a tag to do error checking.
What kind of error checking do we need here?
Thanks.
Like many Unix/POSIX API functions, stat(2)
returns a negative integer on failure. Unfortunately, this integer is always -1 for stat. Hence, you need to check the errno
global variable (defined in <errno.h>
) to see what the exact error was.
Ben has listed some of the errors you can run into; the errno codes for these and other errors are listed in the stat manpage.