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)所获得的字符是不同的两个字符。
追问
对奥!怪不得控制台输出的,跟文件输出的,缺的字符不同!!感谢!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章