我編寫了一個程序在單獨的文件中打印奇數和偶數。我的計劃是在輸出中顯示兩次的值(C編程)
#include<stdio.h>
int main()
{
FILE *f1,*f2,*f3;
int n,i,num;
f1 = fopen("number.txt","w");
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
fprintf(f1,"%d ",i);
fprintf(f1,"\n");
fclose(f1);
f1 = fopen("number.txt","r");
f2 = fopen("even.txt","w");
f3 = fopen("odd.txt","w");
fprintf(f2,"Even numbers:\n");
fprintf(f3,"Odd numbers:\n");
while(!feof(f1)){
fscanf(f1,"%d",&num);
if(num%2 == 0)
fprintf(f2,"%d ",num);
else
fprintf(f3,"%d ",num);
}
fclose(f1);
fclose(f2);
fclose(f3);
return 0;
}
,輸出是
Enter the number:10
$ cat number.txt
1 2 3 4 5 6 7 8 9 10
$ cat even.txt
Even numbers:
2 4 6 8 10 10
$ cat odd.txt
Odd numbers:
1 3 5 7 9
爲什麼我獲得兩個10S在偶數輸出?
[「while(!feof(file))」的可能重複始終是錯誤的](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – 2014-11-21 07:54:39