首先这几个函数的使用方法请移步
fseek 点击打开链接
fread点击打开链接
fwrite点击打开链接
进入正题:
#include<stdio.h>
#include<math.h>
struct record
{
int x,y;
};
int main()
{
FILE *fp=fopen("a.txt","w+");
struct record a={1,2};
struct record b={3,4};
struct record c={5,6};
fwrite((char*)&a,sizeof(struct record),1,fp);
fwrite((char*)&b,sizeof(struct record),1,fp);
fwrite((char*)&c,sizeof(struct record),1,fp);
fseek(fp,0,0);
struct record rec;
while(fread((char*)&rec,sizeof(rec),1,fp)==1)
{
printf("%d %d\n",rec.x,rec.y);
fseek(fp,-(long)sizeof(rec),1);
rec.x*=2;
rec.y*=2;
fwrite((char*)&rec,sizeof(rec),1,fp);
fseek(fp,0,1);//没有这句就死循环了
}
printf("\n");
fseek(fp,0,0);
while(fread((char*)&rec,sizeof(rec),1,fp)==1)
{
printf("%d %d\n",rec.x,rec.y);
}
fclose(fp);
return 0;
}
原文:http://blog.csdn.net/wdkirchhoff/article/details/43791401