0

我有以下结构:

typedef struct PList {
    struct PList* next;
    Person value;
} PList;

typedef struct Person{
        char name[100]; // Left as "" if empty Person
        PersonID ID;
        float amountOwed;
}Person;

typedef struct PersonID{
        char letter;
        int number; // 7 digits
}PersonID;

在我离开程序之前,所有内容都已正确存储,但是在离开(因此保存)然后重新打开(因此加载)之后,Person.ID.number 变为 0

以下是保存和加载部分(客户是 PList):

保存

file = fopen("SaveData/customers.dat", "w");
int a;
    a = lengthPList(&customers);
    fwrite(&a, sizeof(int), 1, file);
    PList* temp;
    temp = &customers;
    for (; a > 0; a--) {
        int b;
        b = sizeof(temp->value);
        fwrite(&b, sizeof(int), 1, file);
        fwrite(&temp->value, sizeof(temp->value), 1, file);
        if (a > 1)
            temp = temp->next;
    }

正在加载

file = fopen(fileName, "r");
int a;
            fread(&a, sizeof(int), 1, file);
            for (; a > 0; a--) {
                Person a;
                int b;
                fread(&b, sizeof(int), 1, file);
                fread(&a, b, 1, file);
                addPersonToList(&a, &customers);
            }

旧代码

保存

file = fopen(fileName, "w");
int a;
a = lengthPList(&customers);
fwrite(&a, sizeof(int), 1, file);
fwrite(&customers, sizeof(customers), a, file);

正在加载

file = fopen(fileName, "r");
int a;
fread(&a, sizeof(int), 1, file);
fread(&customers, sizeof(customers), a, file);
4

0 回答 0