请编写函数,从文件中读取成绩不及格的学生信息,将结果写入另一个文件。
函数接口定义:
void FailStudent(FILE* in, FILE* out);
说明:
(1)参数in和out为指示两个文件的指针。函数从in所指文件中读出数据,将结果写入out所指的文件中。
(2)in所指文件中有很多行(不超过100行),每行对应一名学生的多个信息,以空格间隔,这些信息包括:学号(12位),姓名(不超过10个字符,且其中不含空格),三科成绩:数学,英语,C语言(均是<=100的非负整数)。
(3)函数要求,某科成绩不及格(<60)的学生,需要将其“学号 姓名 该科课程名”三部分内容写入out文件中,每行一个结果。其中,三门课程的课程名分别为:"Math","English","C"。
裁判测试程序样例:
#include<stdio.h>
#define MAXN 100
struct student
{
long long id;
char name[MAXN];
int score[3];
};
void FailStudent(FILE* in, FILE* out);
int main()
{
FILE *in, *out;
in = fopen("in.txt", "r");
out = fopen("out.txt", "w");
if (in && out)
{
FailStudent(in, out);
}
else
{
puts("文件无法打开!");
}
if (in)
{
fclose(in);
}
if (out)
{
fclose(out);
puts("文件保存成功!");
}
return 0;
}
/* 请在这里填写答案 */
输入样例:
“in.txt”文件中保存的数据样例为:
202018010102 zhangsan 50 50 50
202026070420 hanmeimei 90 90 90
202116050331 lilei 50 90 100
202128060211 wangmazi 70 70 59
输出样例:
程序运行结束后,打开“out.txt”文件,查看文件内容。
202018010102 zhangsan Math
202018010102 zhangsan English
202018010102 zhangsan C
202116050331 lilei Math
202128060211 wangmazi C
代码:
void FailStudent(FILE* in, FILE* out)
{
struct student stu;
while(fscanf(in,"%lld %s %d %d %d",&stu.id,stu.name,&stu.score[0],&stu.score[1],&stu.score[2])!=EOF)
{
if (stu.score[0]<60)
fprintf (out,"%lld %s Math\n",stu.id,stu.name);
if (stu.score[1]<60)
fprintf (out,"%lld %s English\n",stu.id,stu.name);
if (stu.score[2]<60)
fprintf (out,"%lld %s C\n",stu.id,stu.name);
}
}
注意:
1. 用结构体来写,比较简单,但还是要注意思路