0

I get the "Error: could not check file size". Any idea why?

 // Generate Content-Length int file_size; int fd; char clenbuf[BUFLEN]; struct stat fs; fd = open(filename, "r"); if (fstat(fd, &fs) == -1) { printf("Error: could not check file size\n"); return; } file_size = fs.st_size; sprintf(clenbuf, "Content-Length: %d", file_size); 
2
  • You forgot to check whether the call to open was successful or not. Also you're getting confused between fopen and open. Try turning on compiler warnings too. Commented Mar 7, 2014 at 0:16
  • 1
    Did you think about checking the result of your fopen() call before just going ahead and using it? It doesn't just magically always manage to open everything you pass it in filename. Commented Mar 7, 2014 at 0:17

2 Answers 2

1

Try it this way:

fd = open(filename, "r"); if (NULL == fd) { printf("Could not open the file\n"); exit(0); } if (fstat(fd, &fs) == -1) { printf("Error: could not check file size\n"); return; } 
Sign up to request clarification or add additional context in comments.

Comments

1

The following include was missing:

#include <fcntl.h> 

2 Comments

That would've resulted in a compile-time error. However, the Q indicates that the program ran, just not as expected. (I won't down-vote, out of respect for the name Chuck Finley)
After I included fcntl.h everything worked just fine. I compiled with -W -Wall and had no warnings :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.