【发布时间】:2011-12-18 18:41:34
【问题描述】:
在这里,当我使用 stat() 获取文件大小时,它会给出不同的输出,为什么会这样?
当“huffman.txt”包含像“Hi how are you”这样的简单字符串时,它会给出file_size = 14。但是当“huffman.txt”包含像“άSUä5Ñ®qøá”F”这样的字符串时,它会给出file size = 30。
#include <sys/stat.h>
#include <stdio.h>
int main()
{
int size = 0;
FILE* original_fileptr = fopen("huffman.txt", "rb");
if (original_fileptr == NULL) {
printf("ERROR: fopen fail in %s at %d\n", __FUNCTION__, __LINE__);
return 1;
}
/*create variable of stat*/
struct stat stp = { 0 };
stat("huffman.txt", &stp);
/*determine the size of data which is in file*/
int filesize = stp.st_size;
printf("\nFile size is %d\n", filesize);
}
【问题讨论】:
-
为什么要先打开文件? stat 不需要那个。
-
并非文件中的所有字符都可以打印,但它们仍在文件中。