最近在學(xué)C語(yǔ)言文件那一章的時(shí)候遇到了一個(gè)不明白的問(wèn)題,請(qǐng)大家?guī)兔匆幌隆?br>
file1.txt是磁盤(pán)中已經(jīng)存在的一個(gè)文件,現(xiàn)將其內(nèi)容復(fù)制到另一文件file2.txt中,程序是這樣的: #include<stdio.h> main() { FILE *in,*out; char c,infile[20],outfile[20]; printf("Please input infile name:"); scanf("%s",infile); printf("Please input outfile name:"); scanf("%s",outfile); if((in=fopen(infile,"r"))==NULL) { printf("cannot open the file!\n"); exit(0); } if((out=fopen(outfile,"w"))==NULL) { printf("cannot open the file!\n"); exit(0); } while(!feof(in)) { c=fgetc(in); fputc(c,out); } fclose(in); fclose(out); } 在輸入file1.txt回車(chē),file2.txt回車(chē)后,發(fā)現(xiàn)file2文件內(nèi)容的末尾多出來(lái)了一個(gè)奇怪的符號(hào), 若將while那一部分修改如下,便不會(huì)再有那個(gè)奇怪的符號(hào)出現(xiàn)了: c=fgetc(in); while(c!=EOF) { fputc(c,out); c=fgetc(in); } 請(qǐng)問(wèn)這是怎么回事??? 問(wèn)題在這里 在文件尾的時(shí)候,這兩句話(huà)一樣會(huì)執(zhí)行 c=fgetc(in); // 此時(shí)已經(jīng)是EOF了,再輸出,就是個(gè)亂碼 fputc(c,out); |
|