在這裏,當我使用stat()
獲得文件大小時,它會給出不同的輸出,爲什麼它的行爲如此呢?當文件具有相同數量的字符時,爲什麼文件大小不同?
當「huffman.txt」包含一個簡單的字符串,如「你好嗎」時,它給出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不需要那個。 –
並非文件中的所有字符都可以打印,但它們仍在文件中。 –