文件操作--文本文件读入

Home Web Board ProblemSet Standing Status Statistics
OJ系统新功能测试中,如有问题请联系 17865569030 17865569180 17865571035 尽量不要在上课时间打电话

Problem B: 文件操作--文本文件读入

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 1696   Solved: 534
[ Submit][ Status][ Web Board]

Description

现有100名学生的姓名(name)、学号(num)、英语(English)、数学(Math)、语文(Chinese)成绩存储在一个文本文件student.dic中(姓名不超过20个字符,学号和各科成绩为整型,各数据之间用空格分隔),现要求将指定行数的学生信息输出,每条信息占一行。

前5行学生信息为:
akdh 13773 84 83 66
fjka 30257 15 14 88
sfhklas 61281 87 8 31
hfu 38635 55 50 60
iwehfk 92803 54 6 77

Input

要输出行号的整数序列,以0作为结束标志。

Output

输出学生信息,每个学生占一行

Sample Input

1 3 5 0

Sample Output

akdh 13773 84 83 66
sfhklas 61281 87 8 31
iwehfk 92803 54 6 77

HINT

打开文件时不需要指定路径

[ Submit][ Status][ Web Board]

代码

#include <stdio.h>
#include <stdlib.h>
#define M "student.dic"
#define ERROR -1
#define NUM 101
typedef struct student{
     char name[20] ;
     int  ID ;
     int   English ;
     int Math ;
     int Chinese ;

}Stu;
int main()
{
    int i =1, j=0 ;
    int n ;
    Stu a[NUM] ;
    FILE *fp = fopen(M,"r");
    if(!fp)
    {
        printf("open the dat failed\n");
        exit(ERROR);
    }
    	for(i= 1; i <101 ;i++)
	{
		fscanf(fp ,"%s %d %d %d %d\n",a[i].name ,&a[i].ID ,&a[i].English ,&a[i].Math ,&a[i].Chinese);


	}
    while(scanf("%d",&n)!=EOF &&n &&(n<=100))
    {

        printf("%s %d %d %d %d\n",a[n].name,a[n].ID,a[n].English,a[n].Math,a[n].Chinese);
    }

    fclose(fp);

    return 0 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41661809/article/details/80821047