首页 > 其他 > 详细

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

时间:2016-09-12 12:14:21      阅读:102      评论:0      收藏:0      [点我收藏+]

#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;
}

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

原文:http://www.cnblogs.com/judes/p/5864221.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!