Hello i had a simple copy file program in C but i cant explain why i get different output in the destination file when i use the 2nd method. The correct output with for loop:
I am the worst programmer in the world!
:D
And this is bla bla bla bla
more bla bla bla...
BUT, with while loop a arbitrary char is produced in EOF:
I am the worst programmer in the world!
:D
And this is bla bla bla bla
more bla bla bla...
法典
int main()
{
int i;
char ch;
create_files();
FILE *src = fopen("best.txt", "r");
FILE *dst = fopen("copied.txt", "w");
for(i=getc(src); i!=EOF; i=getc(src)) //correct copy
{
putc(i, dst);
}
/* while(!feof(src)) //woot?
{
ch=fgetc(src);
fputc(ch,dst);
}*/
fclose(dst);
fclose(src);
return 0;
}
void create_files()
{
FILE *fp;
fp = fopen("best.txt","w");
fprintf(fp,"I am the worst programmer in the world!
:D
And this is bla bla bla bla
more bla bla bla...
");
fclose(fp);
}
i 炉 used既使用烟.,又使用 put和植物,也使用同样方法。 是否忘记了什么?