保存结构体到本地(二进制)

#include <stdio.h>
typedef struct student
{
float score1;
float score2;
float score3;
float avr;
}STUDENT;
void save(STUDENT * student)//以二进制形式保存
{

FILE *fp=NULL;
fp=fopen("test.bin","wb");
if(fp==NULL)
printf("文件为空");
if(fwrite((void*)student,sizeof(STUDENT),1,fp)!=1)
printf("写入失败");
}
int main()
{
int i;
STUDENT stu[3]={{0}};
for(i=0;i<3;i++)
{
printf("请输入第%d个同学的成绩:",i+1);
scanf("%3f %3f %3f",&stu[i].score1,&stu[i].score2,&stu[i].score3);
}
for(i=0;i<3;i++)
{
stu[i].avr=(stu[i].score1+stu[i].score2+stu[i].score3)/3;
printf("第%d个同学的成绩是:%f %5f %5f %5f",i+1,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].avr);
printf("\n");
}
for(i=0;i<3;i++)
save(&stu[i]);
printf("\n");

return 0;
}

posted @ 2016-09-12 11:39  朱小勇  阅读(626)  评论(0编辑  收藏  举报