#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int ID;
char name[20];
float salary;
}employee;
int main()
{
employee e[3] = {{1,"张三",3500},{2,"李四",3600},
{3,"王麻子",3800}};
FILE *fp = NULL;
//"r+" 读和写(文件的头部开始) "w+" 读写(覆盖) "a+" 读写(追加)
if((fp=fopen("d.txt","w+"))==NULL)
{
printf("文件打开失败\n");
return 0;
}
printf("文件打开成功\n");
int count = 0;
//fread,fwrite 返回值:成功进行了多少次读写
if((count=fwrite(e,sizeof(employee),3,fp))<3)
{
printf("写入失败\n");
}
fseek(fp,-3*sizeof(employee),SEEK_CUR);//rewind
employee temp;
int i = 0;
for(;i<3;i++)
{
fread(&temp,sizeof(employee),1,fp);
printf("工号:%d 姓名:%s 薪资:%g\n",temp.ID,temp.name,
temp.salary);
}
fclose(fp);
system("PAUSE");
return 0;
}