Problem A: File Operations -- Binary File Reading

Problem A: File Operations -- Binary File Reading

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 2232  Solved: 631
[Submit][Status][Web Board]

Description

The names (name), student numbers (num), English (English), mathematics (Math), and Chinese (Chinese) of the existing 100 students are stored in a binary file student.dic (the name is char[20], and the student. The number and the grades of each subject are stored in int), and now it is required to output the student information of the specified number of lines, and each piece of information occupies one line.

The first 5 lines of student information are:
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

Sequence of integers to output line numbers, terminated by 0.

Output

Output student information, each student occupies one line

Sample Input

1 3 5 0

Sample Output

akdh 13773 84 83 66
sfhklas 61281 87 8 31
iwehfk 92803 54 6 77
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
    char name[20];
    int num;
    int English;
    int Math;
    int Chinese;
};
intmain()
{
    struct student str[100];
    int number,i=0;
    FILE *fp;
    if((fp=fopen("student.dic","rb"))==NULL)
    {
        printf("Cannot open file!");
        exit(1);
    }
    else
    {
        while(fread(&str[i],sizeof(struct student),1,fp)!=0)//Binary read function
        {
            i++;
        }
        fclose(fp);
        while(scanf("%d",&number)!=EOF)
        {
            if(number==0)
            break;
            printf("%s %d %d %d %d\n",str[number-1].name,str[number-1].num,str[number-1].English,str[number-1].Math,str[number-1].Chinese);
        }
    }
    return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324608925&siteId=291194637