chuckfinley
chuckfinley

Reputation: 2595

C: check file size

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);

Upvotes: 0

Views: 150

Answers (2)

chuckfinley
chuckfinley

Reputation: 2595

The following include was missing:

#include <fcntl.h>

Upvotes: 1

abelenky
abelenky

Reputation: 64730

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;
  }

Upvotes: 1

Related Questions