C語言 關於fgetc 函數
C語言 關於fgetc 函數 百度知道問題 已經答案
#include "stdafx.h"
#include "stdio.h"
int main(int argc, char* argv[])
{
FILE*fp,*fp2;
fp=fopen("D:\\C\\file5\\file.txt","r");
fp2=fopen("D:\\C\\file5\\file2.txt","w");
if(fp==NULL)
printf("cannot open file");
if(fp2==NULL)
printf("cannot open file2");
char ch;
while(!feof(fp))
{
ch=fgetc(fp);
putchar(ch);
fputc(ch,fp2);
}
fclose(fp);
fclose(fp2);
return 0;
}
如果while循環裏改爲:
while(!feof(fp))
{
putchar(fgetc(fp));
fputc(fgetc(fp,fp2);
}
輸出的就不對了,就缺失字母
file的內容:
abcdefg
hijklmn
opqrstu
vwxyz
每次運行fgetc,都會重新去獲得一次字符,你在putchar(fgetc(fp));中的fgetc所獲得的字符,和fputc(fgetc(fp,fp2)所獲得的字符是不同的兩個字符。
追問
對奧!怪不得控制檯輸出的,跟文件輸出的,缺的字符不同!!感謝!
#include "stdafx.h"
#include "stdio.h"
int main(int argc, char* argv[])
{
FILE*fp,*fp2;
fp=fopen("D:\\C\\file5\\file.txt","r");
fp2=fopen("D:\\C\\file5\\file2.txt","w");
if(fp==NULL)
printf("cannot open file");
if(fp2==NULL)
printf("cannot open file2");
char ch;
while(!feof(fp))
{
ch=fgetc(fp);
putchar(ch);
fputc(ch,fp2);
}
fclose(fp);
fclose(fp2);
return 0;
}
如果while循環裏改爲:
while(!feof(fp))
{
putchar(fgetc(fp));
fputc(fgetc(fp,fp2);
}
輸出的就不對了,就缺失字母
file的內容:
abcdefg
hijklmn
opqrstu
vwxyz
每次運行fgetc,都會重新去獲得一次字符,你在putchar(fgetc(fp));中的fgetc所獲得的字符,和fputc(fgetc(fp,fp2)所獲得的字符是不同的兩個字符。
追問
對奧!怪不得控制檯輸出的,跟文件輸出的,缺的字符不同!!感謝!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.