#include <stdio.h>
void main(int argc,char *argv[]){
FILE *in,*out;
int i,n;
printf("please input the smallest number that should be copied:");
scanf("%d",&n);
if(argc!=3)
printf("The number of arguments is not correct!\n");
if((in=fopen(argv[1],"r"))==NULL)
printf("Can not open the source file!\n");
if((out=fopen(argv[2],"w"))==NULL)
printf("Can not create the destination file!\n");
while(!feof(in)){
fread(&i,sizeof(int),1,in);
if(i>=n)
fwrite(&i,sizeof(int),1,out);
}
fclose(in);
fclose(out);
}
我创建了一个a.dat文件,文件里是126 127 128 129 130 131 132 133 134,我输入了130,结果却把126~130都复制到了另一个文件中。用Debug查看后发现实际读取的 i 的值很大,大约为171323953、171389489……
请各位高手帮我看看问题出在哪里,再次感谢各位高手!!!