一道c语言编程题(关于文件)

来源:百度知道 编辑:UC知道 时间:2024/05/27 01:39:34
有一磁盘文件“score.txt”存放20名学生的各科成绩。每个学生的数据包括:学号、数学、语文、英语三门课成绩。要求计算所有学生的平均成绩(用函数实现),然后生成新文件“avgscore.txt”存放学号和平均成绩。
T T 怎么把score中的东西放到结构体里。。。

#include <stdlib.h>
#include <stdio.h>

struct student
{
long num;
int math, chinese, english;
};

float average(struct student *stud)
{
return (float)(stud->math + stud->chinese + stud->english) / 3;
}

void main()
{
FILE *infile, *outfile;
infile = fopen("score.txt", "r");
outfile = fopen("avgscore.txt", "a+");
struct student s;

if (!infile || !outfile)
{
printf("Error!\n");
return;
}

while (1)
{
s.num = 0;
fscanf(infile, "%ld %d %d %d", &s.num, &s.math, &s.chinese, &s.english);

if (s.num == 0)
break;

fprintf(outfile, "%ld %.2f\n", s.num, average(&s));
}

fclose(infile);
fclose(outfile);
system("PAUSE");
}

提示:
运用结构体和文件知识
s