我试图替换二进制文件中的数据,但是在我打印数据时更换后,而不是只打印2个数据,打印3个数据,其中1个数据正在重复
就像feof()
无法正常处理二进制文件
#include <stdio.h>
main(void) {
FILE* outfile;
outfile = fopen("c:\\in.dat","wb");
int data=20;
int* p =&data;
fwrite(p,sizeof(int),1,outfile);
data=40;
fwrite(&data,sizeof(int),1,outfile); //this will be replaced by int x
int x=100;
long int indicate = sizeof(int);
fseek(outfile,indicate,SEEK_SET);
fwrite(&x,sizeof(int),1,outfile);
fclose(outfile);
FILE* infile;
infile = fopen("c:\\in.dat","rb");
while(!feof(infile)) {
fread(&data,sizeof(int),1,infile);
printf("%d\n",data);
}
return;
}