这到底是怎么一回事,请高手帮忙?
#include<stdio.h>main()
{
FILE *fp,*fp2;char ch;
fp = fopen("1.txt","r");
fp2 = fopen("12.txt","w");
for(;(ch = getc(fp)!=EOF);)
putc(ch, fp2);
}
执行完后,我打开12。txt 发现里面全是 一个个方框的字符 why?这不是我想要的。
#include<stdio.h> main() { FILE *fp,*fp2;char ch; fp = fopen("1.txt","r"); fp2 = fopen("12.txt","w"); for(;(ch = getc(fp)!=EOF);)//应是(ch = getchar())!='0';注意优先级别。还有ch是字符类型,用返回EOF判断是个陷阱, //他容不下EOF。你可以把ch定义为int. putc(ch, fp2); //这里是fputc函数 }我改了一下:
#include<stdio.h> main() { FILE *fp,*fp2; char ch; fp2 = fopen("12.txt","w"); for(;(ch = getchar())!='0';) fputc(ch, fp2); }
#include<stdio.h> int main() { FILE *fp,*fp2; int ch; fp = fopen("1.txt","r"); fp2 = fopen("12.txt","w"); while((ch=fgetc(fp)) != EOF) fputc(ch, fp2); return 0; }