C language reads the specified student information in the file (enter student ID and print personal information)

Don't look forward to tomorrow, don't miss yesterday, to grasp today. Don't look forward to tomorrow, don't remember yesterday, just grasp today.

Title description:

Read and display the student information of the designated student number from the text. The storage format of the text file is that each line corresponds to one student information, and the last line has no line break.

Input
solve(char s[]) has given a string s, which represents the student number.
The file a.txt stores all student information.

Outputting
outputs the number of students learning information if there is no output Not Found!
Sample input:
11405200102
The contents of a.txt:
11,405,200,101 zhangsan 8011405200102 70 240 80 90 210 70 70 60 80 Lisi
sample output:
11405200102 80 60 70 Lisi 210 70

The source code is as follows:

#include<stdio.h>
#include<string.h>
extern void solve();
int main(void)
{
    
    
    FILE *f=fopen("a.txt","w");
    fprintf(f, "%s\n", "11405200101 zhangsan 70 80 90 2140 80");
    fprintf(f, "%s\n", "11405200102 lisi 80 60 70 210 70");
    fprintf(f, "%s\n", "11405200103 lisasdsi 80 10 70 220 70");
    fprintf(f, "%s\n", "11405200104 liassi 80 60 70 240 70");
    fprintf(f, "%s\n", "11405200105 lzissssi 80 30 76 210 70");
    fprintf(f, "%s\n", "11405200106 lisgsi 80 50 70 210 70");
    fprintf(f, "%s\n", "11405200107 lizssi 80 67 70 280 76");
    fprintf(f, "%s\n", "11405200108 lisqsi 80 60 70 210 70");
    fprintf(f, "%s", "11405200109 lisdssi 80 67 70 210 70");
    fclose(f);
    char s[100];
    scanf("%s",&s);
    solve(s);
    return 0;
}

void solve(char s[]) {
    
    
    FILE *fq = fopen("a.txt", "r");
    int n;
    char line[20], a[100];
    for (n = 0; n < 9; n++) {
    
    
        fgets(line, 12, fq);
        fgets(a, 100, fq);
        if (strcmp(s, line) == 0) {
    
    
            printf("%s %s", line, a);
            fclose(fq);
            break;
        }
        if (n == 8) {
    
    
            printf("Not Found!");
        }
    }
}

The results of the operation are as follows:
Insert picture description here
Final words: Brush the questions to find your own shortcomings, and then go to specialize.

Guess you like

Origin blog.csdn.net/m0_46259251/article/details/106210205