Alex ander bro 2024-06-08 17:21 采纳率: 0%
浏览 0

c语言学生学籍管理系统

在我用c语言链表来编写学生学籍管理系统的时候,出现了这样的问题:第一次运行该代码能够实现所有的功能包括查找,但是第二次运行的时候执行查找操作的时候发现查找不到任何信息,所以我认为我写的加载文件的函数出现了错误,以下是我的代码片段,包含了结构体,存储结构和加载文件的函数。

struct StudentInfo {
    char name[50];
    int id;
    char sex[30];
    int age;
    char major[50];
    int sort;
    float grapoint;
};
struct Course {
    char name[20];
    float score;
    float credit;
    float DKgrapoint;
    int ismakeup;//用来区分补缓考
};
typedef  struct student
{
    struct StudentInfo  info;
    struct Course  courses[10];
}stu;
void load(linklist*& L) //运行前把文件内容读取到电脑内存
{
    FILE* file = fopen("student.txt", "r"); // 打开包含学生信息的文件
    if (file == NULL) {
        printf("无法打开文件\n");
        return;
    }
    linklist* current = NULL;
    while (!feof(file)) {
        linklist* node = (linklist*)malloc(sizeof(linklist));
        if (fread(&(node->data), sizeof(stu), 1, file) != 1) {
            free(node);
            break;
        }
        node->next = NULL;
        if (L== NULL) {
            L= node;
            current =L;
        }
        else {
            current->next = node;
            current = current->next;
        }
    }
    fclose(file);
    printf("信息装载成功:\n");


  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-06-08 17:21
    关注

    引自免费微信小程序:皆我百晓生

    从您提供的代码来看,加载文件的逻辑看起来是正确的。但为了更准确地找出问题所在,我们可以尝试添加一些调试信息,并检查文件读取是否正常。这里我提供了一个修改后的load函数,增加了对每个学生信息的打印,以便在程序运行时观察数据是否正确读取。

    #include <stdio.h>
    #include <stdlib.h>
    
    // ...其他相关头文件和结构体定义...
    
    void load(linklist*& L)
    {
        FILE* file = fopen("student.txt", "r");
        if (file == NULL) {
            printf("无法打开文件\n");
            return;
        }
    
        linklist* current = NULL;
        while (!feof(file)) {
            linklist* node = (linklist*)malloc(sizeof(linklist));
            if (fread(&(node->data), sizeof(stu), 1, file) != 1) {
                free(node);
                printf("读取学生信息失败\n");
                break;
            }
    
            node->next = NULL;
    
            if (L == NULL) {
                L = node;
                current = L;
            } else {
                current->next = node;
                current = current->next;
            }
    
            // 添加调试输出
            printf("加载的学生信息:\n");
            printf("姓名: %s\n", node->data.info.name);
            printf("学号: %d\n", node->data.info.id);
            printf("性别: %s\n", node->data.info.sex);
            printf("年龄: %d\n", node->data.info.age);
            printf("专业: %s\n", node->data.info.major);
            printf("排序: %d\n", node->data.info.sort);
            printf("绩点: %.2f\n", node->data.info.grapoint);
    
            for (int i = 0; i < 10 && node->data.courses[i].name[0] != '\0'; i++) {
                printf("课程名: %s, 成绩: %.2f, 学分: %.2f, 补缓考标志: %d\n",
                       node->data.courses[i].name,
                       node->data.courses[i].score,
                       node->data.courses[i].credit,
                       node->data.courses[i].ismakeup);
            }
            printf("\n");
        }
    
        fclose(file);
        printf("信息装载成功:\n");
    }
    

    请将您的 load 函数替换为上述代码,然后重新运行程序。当程序运行时,它会显示每次加载的学生信息,这将有助于我们确认文件是否被正确读取。如果所有学生的信息都能正确显示,那么问题可能在于其他部分的代码(例如查找功能)。如果在第二次运行时仍没有输出,那可能是文件读取存在问题。如果有输出但查找仍然不工作,那么问题可能在于查找算法或数据结构的遍历上。

    如果问题依然存在,请提供更多的上下文,特别是查找功能的代码,这样我可以进一步帮助您分析问题。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 6月8日