#include <stdio.h>
typedef struct
{
char num[6];
char name[8];
int score[6];
float avr;
}STUDENT; //定义结构体
STUDENT student[5]; //定义结构体数组
int main(int argc, const char * argv[]) {
int i, j, sum;
FILE *fp;
// 输入5个学生信息
for (i = 0; i < 3; i++) {
printf("请输入第%d个同学的信息:\n", i+1);
printf("stuNO:");
scanf("%s", student[i].num);
printf("\nname:");
scanf("%s", student[i].name);
sum = 0;
//求出平均成绩
for (j = 0; j < 3; j++) {
printf("score%d:", j+ 1);
scanf("%d", &student[i].score[j]);
sum += student[i].score[j];
}
student[i].avr = sum/3.0;
printf("该学生平均成绩:\n%3.2f\n", student[i].avr);
}
fp = fopen("stud", "wb+"); //打开文件
for (i = 0; i < 3; i++) {
//将学生信息写入文件
if (fwrite(&student[i], sizeof(STUDENT), 1, fp) != 1) {
printf("file write error\n");
}
}
fclose(fp); //关闭文件
}