我怎么传递了错误的参数?

问题描述 投票:0回答:0

我有一个文件,它接受

studentInfo
并创建一个
studentRecord
结构并将其存储在可以显示的列表中。我可以将结构添加到列表中并对其进行排序,没有问题。我也可以毫无问题地显示列表。我的问题出在
save()
函数和
load()
函数上。

save()
功能已提供给我,不应更改。
load()
函数是我需要创建的,但最终却出现了错误。

save()
功能:

void save(char* fileName)
{
    FILE* file;
    int i, schoolYearValue = 0;
    file = fopen(fileName, "wb");       // open file for writing

    // First, store the number of students in the list
    fwrite(&count, 1, sizeof(count), file);

    // Parse the list and write student record to file
    for (i = 0; i < count; i++)
    {
        fwrite(list[i].studentName, sizeof(list[i].studentName), 1, file);
        fwrite(list[i].major, sizeof(list[i].major), 1, file);
        // convert enum to a number for storing
        if (list[i].schoolYear == freshman)
            schoolYearValue = 0;        // 0 for freshman
        else if (list[i].schoolYear == sophomore)
            schoolYearValue = 1;        // 1 for sophomore
        else if (list[i].schoolYear == junior)
            schoolYearValue = 2;        // 2 for junior 
        else
            schoolYearValue = 3;        // 3 for senior

        fwrite(&schoolYearValue, sizeof(schoolYearValue), 1, file);
        fwrite(&list[i].studentID, sizeof(list[i].studentID), 1, file);
    }

    fclose(file);           // close the file after writing
}

我的

load()
函数如下所示:

void load(char* fileName)
{
    FILE* file; 
    int n, i;
    file = fopen(fileName, "rb");
    fread(&n, sizeof(count), 1, file);
    char studentName_input[MAX_NAME_LENGTH];
    char major_input[MAX_NAME_LENGTH];
    char schoolYear_input[20];
    schoolYear schoolYearValue;
    unsigned int studentID_input;
    for (i = 0; i < n; i++)
    {
        fread(studentName_input, sizeof(studentName_input), 1, file);
        fread(major_input, sizeof(major_input), 1, file);
        fread(&schoolYearValue, sizeof(schoolYearValue), 1, file);
        if (schoolYearValue == freshman)
            strcpy(schoolYear_input, "freshman");
        if (schoolYearValue == sophomore)
            strcpy(schoolYear_input, "sophomore");
        if (schoolYearValue == junior)
            strcpy(schoolYear_input, "junior");
        if (schoolYearValue == senior)
            strcpy(schoolYear_input, "senior");

        fread(&schoolYear_input, sizeof(schoolYear_input), 1, file);
        fread(&studentID_input, sizeof(studentID_input), 1, file);
        
        addSort(studentName_input, major_input, schoolYear_input, studentID_input);
    }
}

这是我在第 268 行遇到的错误:

将无效参数传递给认为无效参数致命的函数。

它所指的代码行是

fread(&n, sizeof(count), 1, file);

这有什么问题吗? (PS:我是 C 新手,所以如果这是一个愚蠢的错误,我深表歉意。)

有人告诉我尝试将变量

i
放入数组中,这样它实际上可以保存该值,但我不确定那会是什么样子,或者是否有必要。

c fwrite fread
© www.soinside.com 2019 - 2024. All rights reserved.