GINAjulie 2023-12-01 16:27 采纳率: 20%
浏览 3

vs2019 无法打开文件

img

img


文件读取失败,vs2019 无法打开文件,是源文件这里错了吗?

void readStudentFile(struct Student** head) {
    FILE* file = fopen("students.txt", "r");
    if (file == NULL) {
        printf("无法打开文件\n");
        return;
    }

    char line[100]; // 假设每行最多100个字符
    //读文件,写文件
    while (fgets(line, sizeof(line), file) != NULL) {
        char studentId[10], name[50], password[20];
        int balance;

        if (sscanf(line, "%s %s %s %d", studentId, name, password, &balance) == 4) {
            // 格式正确,创建新的学生节点
            struct Student* newStudent = (struct Student*)malloc(sizeof(struct Student));
            if (newStudent == NULL) {
                printf("内存分配失败\n");
                fclose(file);
                return;
            }
            // 将学生信息复制到新节点中
            strcpy(newStudent->studentId, studentId);
            strcpy(newStudent->name, name);
            strcpy(newStudent->password, password);
            newStudent->balance = balance;
            newStudent->next = NULL;
            // 将新的学生节点添加到链表中
            addStudent(head, newStudent);
        }
        else {
            printf("文件格式错误\n");
        }
    }

    fclose(file);
}


  • 写回答

3条回答 默认 最新

  • 一定会升职加薪 2023-12-01 17:58
    关注

    应该是文件的相对路径不对,你可以直接使用绝对路径。

    评论

报告相同问题?

问题事件

  • 创建了问题 12月1日

悬赏问题

  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真
  • ¥15 关于#c语言#的问题,请各位专家解答!
  • ¥15 这个如何解决详细步骤
  • ¥15 在微信h5支付申请中,别人给钱就能用我的软件,这个的所属行业是啥?