假设有若干学生,每个学生有3门功课,从键盘上输入学生有关信息(学号、姓名、成绩),并计算其平均成绩,将原有数据和平均分保存在磁盘文件record中。具体要求如下: 学生有关信息采用结构体类型存储。 fwrite()函数实现数据块输出到文件record。 结合循环结构实现数据输入和输出。用C语言写出来
时间: 2023-03-10 07:01:18 浏览: 177
我可以使用以下C语言代码来满足您的要求: #include <stdio.h>
#include <stdlib.h>struct student {
int num;
char name[20];
float score[3];
};int main()
{
int n, i;
float avg;
struct student stu;
FILE *fp; printf("Please enter the number of students:");
scanf("%d", &n); fp = fopen("record", "w"); for(i=0; i<n; i++)
{
printf("Please enter the num:");
scanf("%d", &stu.num);
printf("Please enter the name:");
scanf("%s", stu.name);
printf("Please enter the three scores:");
scanf("%f %f %f", &stu.score[0], &stu.score[1], &stu.score[2]); avg = (stu.score[0] + stu.score[1] + stu.score[2])/3; fwrite(&stu, sizeof(stu), 1, fp);
fprintf(fp, "%f\n", avg);
} fclose(fp); return 0;
}