C:检查文件大小

时间:2014-03-07 00:14:21

标签: c unix

我收到“错误:无法检查文件大小”。知道为什么吗?

  // 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 个答案:

答案 0 :(得分:1)

以这种方式尝试:

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

答案 1 :(得分:1)

以下内容遗失:

#include <fcntl.h>