#include <stdio.h>
int main() {
FILE *fp;
char ch;
char data[100];
int i;
fp = fopen("file.txt","r");
i=0;
while((ch=fgetc(fp)) != EOF) {
data[i]=ch;
i++;
}
i=0;
while(data[i]) {
printf("%c",data[i]);
i++;
}
return 0;
}
內容file.txt的的:文件處理:在輸出額外的字符
[email protected] /opt/lampp/htdocs $ cat file.txt
aGVsbG9teW5hbWVpc2toYW4K
程序的輸出:
[email protected] /opt/lampp/htdocs $ sudo vim test.c
[email protected] /opt/lampp/htdocs $ sudo gcc test.c
[email protected] /opt/lampp/htdocs $ ./a.out
aGVsbG9teW5hbWVpc2toYW4K
P�[email protected] /opt/lampp/htdocs $
爲什麼出現在陣列的輸出這兩個額外的字符.. ??? 輸入文件實際上是base-64編碼的結果。
'ch'應聲明'int'。作爲'char',它的實現使用'unsigned char',與'EOF'的比較永遠不會是真的。 – pmg