2011-10-31 72 views
0

在這裏,當我使用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); 
} 
+0

你爲什麼先打開文件? stat不需要那個。 –

+0

並非文件中的所有字符都可以打印,但它們仍在文件中。 –

回答

1

讀了這一點得到了與編碼做。

純文本英文字符的ASCII碼,其中每個字符是一個字節編碼。 然而,在非純文本字符英文是用Unicode編碼,每個都是2字節。

看看發生了什麼

最簡單方法是使用打印

char c; 
/* Read file. */ 
while (c = fgetc()) 
    printf ("%c", c) 

你就會明白爲什麼文件的大小是不同的每個字符。

0

如果你問爲什麼有相同數目的字符不同的字符串可以有不同的尺寸字節,在UTF-8

相關問題