C language version student management system

Ideas

Enter the load file, display the menu, and select the function. If you do not choose to exit, use binary reading and writing files in the while loop to implement the functions of adding, viewing all students, and searching for students .

Display_Menu()菜单

Load_File()加载文件

Add_Student()新增学生方法:
使用二进制追加读和写打开
判断是否有相同的学号,没有的话写入数据到文件中。

Student_Information()查看全部学生信息:
使用while读取,读取数量不为0则一直读取输出打印。

Lookup_Student()查找学生
使用循环读取数据,在循环中判断输入的学号是否与文件中的学号相同,相同则不写入文件中,不相同即写入文件中。

renderings

Insert image description here
Insert image description here

Menu Bar

void Display_Menu(){
    
    
    printf("=================================================\n");
    printf("=               1.新增学生                        =\n");
    printf("=              2.查看全部学生                      =\n");
    printf("=               3.查找学生                        =\n");
    printf("=              4.重新加载文件                      =\n");
    printf("=                 5.退出                         =\n");
    printf("=================================================\n");
}

main function

int main() {
    
    
    FILE *fp;
    char option;
    char szPath[20];
    printf("请输入需要加载的文件:");
    scanf("%s",&szPath);
    getchar();
    fp = fopen(szPath,"ab+");
    if(fp == NULL){
    
    
        printf("打开失败\n");
        exit(0);
    }
    Display_Menu();
    scanf("%c",&option);

    while(option != '5'){
    
    
        switch (option) {
    
    
            case '1':
                Add_Student(szPath);break;
            case '2':
                Student_Information(szPath);break;
            case '3':
                Lookup_Student(szPath);
                break;
            case '4':
                printf("请输入需要加载的文件:");
                scanf("%s",szPath);
                Load_File(szPath);
                break;
            case '5':
                fclose(fp);
                exit(0);
            default:
                exit(0);
        }
        Display_Menu();
        getchar();
        scanf("%c",&option);
    }
    fclose(fp);
    return 0;
}

File loading

int Load_File(char filename[20]){
    
    
    FILE *fp;
    fp = fopen(filename,"ab+");
    if(fp == NULL){
    
    
        printf("打开失败\n");
    }else{
    
    
        printf("加载成功\n");
    }
    fclose(fp);
    return 0;
}

Add new student

fopen(filename, "ab+") uses binary append reading and writing to open
Check_Student(st->no, filename) to determine whether there are the same student IDs. If not, write the data to the file.

int Add_Student(char *filename){
    
    
    struct Student st;
    FILE *fp = fopen(filename,"ab+");
    printf("==新增学生界面==\n");
    printf("请输入学生信息\n");
    printf("学号:");
    scanf("%d",&st.no);
    printf("姓名:");
    scanf("%s",st.name);
    getchar();
    printf("性别(F:女,M:男):");
    scanf("%c",&st.sex);
    printf("年龄:");
    scanf("%d",&st.age);

    if(Check_Student(st.no,filename)){
    
    
        fwrite(&st,sizeof(struct Student),1,fp);
        printf("成功\n");
        fclose(fp);
        return 0;
    }else{
    
    
        printf("该学生已经存在\n");
        fclose(fp);
        return 0;
    }

}

View all students

Use while to read, if the number of reads is not 0, read the next line of data.

int Student_Information(char *filename){
    
    
    struct Student *st;
    FILE *fp = fopen(filename,"rb+");
    rewind(fp);
    while (fread(st, sizeof(struct Student),1,fp) != 0){
    
    
        printf("%s-%d-%d-%s\n", st->name, st->no, st->age, st->sex == 'F' ? "女" : "男");
    }
    fclose(fp);
    return 0;
}

Find students

Use a loop to read data, and determine whether the entered student ID is the same as the student ID in the file. If they are the same, they will not be written to the file.

int Lookup_Student(char *filename){
    
    
    int szNo;
    struct Student *st;
    printf("请输入要查找的学生学号:");
    scanf("%d",&szNo);
    FILE *fp = fopen(filename,"rb+");
    while (fread(st, sizeof(struct Student),1,fp) != 0){
    
    
        if(st->no == szNo){
    
    
            printf("%s-%d-%d-%s\n", st->name, st->no, st->age, st->sex == 'F' ? "女" : "男");
        }
    }
    fclose(fp);
    return 0;
}

code address

Github:https://github.com/Ltike/Learning.git

Guess you like

Origin blog.csdn.net/qq_43802454/article/details/121297692