OJ 2924 Problem  A 文件操作--二进制文件读入

来源:互联网 发布:cp交易网络是什么意思 编辑:程序博客网 时间:2024/05/17 07:45

Description

现有100名学生的姓名(name)、学号(num)、英语(English)、数学(Math)、语文(Chinese)成绩存储在一个二进制文件student.dic中(姓名用char[20],学号和各科成绩用int存储),现要求将指定行数的学生信息输出,每条信息占一行。

前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 66sfhklas 61281 87 8 31iwehfk 92803 54 6 77
C语言代码:
#include<stdio.h>#include<malloc.h>typedef struct{    char name[20];    int num;    int english;    int math;    int chinese;}Stu;int main(){    FILE *fp;    Stu s[100];    int i=0,n;    fp=fopen("student.dic","rb");    while(!feof(fp))    {        fread((void *)&s[i],sizeof(Stu),1,fp);        ++i;    }    while(scanf("%d",&n),n)    {        n-=1;        printf("%s %d %d %d %d\n",s[n].name,s[n].num,s[n].english,s[n].math,s[n].chinese);    }    fclose(fp);    return 0;}
这题很简单,只要注意二进制文件不能用fscanf去读取,用fread就好了。
0 0
原创粉丝点击