读/写结构文件 - ç [英] read / write structures to file - c

查看:125
本文介绍了读/写结构文件 - ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在C.创建一个学生数据库,我需要能够做到读写创建一个文件数据库的最后一件事。所以,我已经有了一个数组的指针满学生的结构,我需要把它写入一个文件。一旦我有它写,我需要能够读取它放回我的数组为好。

I'm working on creating a student database in C. The final thing I need to be able to do read and write the database I create to a file. So I've already got an array full of pointers to student structures, and I need to write it to a file. Once I have it written, I need to be able to read it back into my array as well.

我真的不知道该怎么做,虽然。这是我的结构:

I'm really not sure how to do it though. This is my struct:

typedef struct student Student;
struct student 
{
    char name[300];
    int age;
    char course1[300];
    char course2[300];
    char remarks[300];
};

Student *Data[30];

这些都是我写与文件中工作的功能:

And these are the functions I've written to work with file:

void writeDisk()
{
    FILE *file = fopen("disk.dat", "ab");
    fwrite(&Data, sizeof(Student), count, file);
    fclose(file);
}

void loadDisk()
{
    FILE *file = fopen("disk.dat", "rb");
    if (file != NULL)
    {
        fread(&Data, sizeof(Student), count, file);
        fclose(file);       
    }
}

void emptyDisk()
{

    FILE *file = fopen("disk.dat", "rw");
    fopen("disk.dat", "wb");

}

我可以看到我的disk.dat文件的大小而变化,当我写它,它变成零的时候我叫空,但负载不会在所有的工作。如果我在程序的学生数组是零,它只是停留在零当我打电话负载,并尝试以显示它,但如果有东西在数组中,我得到一个分段错误,当我打电话负载,然后尝试以显示它。

I can see that the size of my disk.dat file changes when I write to it, and it goes to zero when I call empty, but loading does not work at all. If the student array in my program is zero, it just stays at zero when I call load and try to display it, but if there is something in the array, I get a segmentation fault when I call load and then try to display it.

我可能会做这完全是错误的。我真的不知道我在做什么。我在想,我可以只写全阵列的文件,但我不知道这是真的。我只是想知道,如果有人可以看看这个,让我知道我要去哪里错了。

I may be doing this entirely wrong. I'm really not sure what I'm doing. I was thinking that I could just write the whole array to a file, but I'm not sure that's true. I was just wondering if someone could look at this and let me know where I'm going wrong.

修改

我已经编辑我的code看起来是这样的:

I've edited my code to look like this:

void writeDisk()
{
    int i = 0;
    FILE *file = fopen("disk.dat", "ab");

    for(i; i <count; i++)
    {
        fwrite(Data[i], sizeof(Student), 1, file);  
    }

    fclose(file);
}

void loadDisk()
{
    char buffer[300];
    int i = 0;

    clearData();

    FILE *file = fopen("disk.dat", "rb");
    while(Data[i] != NULL)
{
    Data[i] = malloc(sizeof(Student));
    fread(Data[i], sizeof(Student), 1, file);
    i++;
}

    fclose(file);       
}

这仍然没有工作,虽然。写似乎更好地工作,但我没有看到它写在学生岁以上的文件。在加载文件的clearData()函数只是清除任何的数据阵列中,首先,让我们能有一个干净的石板读取该文件之中。

This still doesn't work though. The write seems to work better,but I don't see it writing the age of the student over to the file. The clearData() function in the load file just clears anything that's in the Data array to begin with, so that we can have a clean slate to read the file into.

推荐答案

我相信,而不是

Student *Data[30];

您想

Student Data[30];

由于第一个是一个指针数组,而第二个是所需的结构体的阵列

Because the first one is an array of pointers, while the second one is an array of the struct you want.

当你写

fread(&Data, sizeof(Student), count, file);

它从文件中读取数据直接进入数据的位置。它看起来像你想读,写的实际结构,所以你必须把它们直接进入阵,而不是使用指针。

It reads the data from the file right into the location of Data. It looks like you want to read and write the actual structs, so you have to put them directly into the array, as opposed to using pointers.

这篇关于读/写结构文件 - ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆