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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 theerrnoglobal 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.