c语言 编写程序,把给定整数文件中所有大于某值的数复制到另一个文件中。 我这个有错误,请各位高手帮看看

#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……
请各位高手帮我看看问题出在哪里,再次感谢各位高手!!!

程序本身没有问题,数据文件a.dat格式不对。fwrite及fread是读二进制文件的,不能对文本文件进行操作,你先执行下面的程序生成二进制文件a.dat,你的程序就可以正常执行了。但查看a.bat及b.bat要用二进制编辑工具C32ASM等查看。
#include <stdio.h>
void main(int argc,char *argv[]){
FILE *out;
int i;
if((out=fopen("c:\\a.dat","wb"))==NULL)
printf("Can not open the source file!\n");
for(i=126;i<=134;i++)
fwrite(&i,sizeof(int),1,out);
fclose(out);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答