#include<stdio.h> typedef struct Stu{ char StuNo[8]; char Name[20]; float iScore[3]; }Student; void main() { FILE *fp; Student stu[2]; float AveScore=0; int i; printf("Please input the students' data...\n"); printf("Stu.No. Name Score1 Score2 Score3\n"); scanf("%s%s",stu[0].StuNo,stu[0].Name); for(i=0;i<3;i++) { scanf("%f",&stu[0].iScore[i]); AveScore+=stu[0].iScore[i]; } AveScore=AveScore/3; //好的方法是将平均值也放入结构体 fp=fopen("stud.dat","rb+"); fwrite(&stu[0],sizeof(Student),1,fp);//因为fwrite写入的为二进制文件,用记事本看是乱码 rewind(fp); fread(&stu[1],sizeof(Student),1,fp); printf("%s %s %.1f %.1f %.1f\n",stu[1].StuNo,stu[1].Name,stu[1].iScore[0],stu[1].iScore[1],stu[1].iScore[2]); fclose(fp); }
0.0分
0 人评分