fread'ing和fwrite'ing包含指针的结构 [英] fread'ing and fwrite'ing a structure that contains pointers

查看:82
本文介绍了fread'ing和fwrite'ing包含指针的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gcc (GCC) 4.7.0
c89

你好

我要尝试重写和读取以下结构.

I have the following structure that I am trying to fwrite and fread.

但是,因为我的设备和资源都是指针. fwrite将读取指针值而不是数据.我无法将数组用于设备或资源.仅指针,因为它们必须动态分配.

However, because my device and resource are pointers. The fwrite will read the pointer values and not the data. I cannot use a array for the device or resource. Only pointers as they have to be dynamically allocated.

我在写之前为结构元素分配了所有内存.由于我想使代码段简短,因此此处未显示.也不是免费的.

I allocate all memory for the structure elements before I write. Not shown here as I want to keep the snippet short. Nor is free'ing.

在我的fread函数中,我为设备和资源分配了内存,以便将fread读入这些内存位置.但是,这将不起作用.

In my fread function, I allocate the memory for the device and resource so that the fread will read into these memory locations. However, this will not work.

做到这一点的最佳方法是什么?

What is the best way to do this?

非常感谢您的任何建议,

Many thanks for any advice,

struct data {
    int id;
    int set;
    char *device;
    char *resource;
};

struct database {
    struct data **db_data;
    size_t database_rows;
    size_t database_data_size;
};

int database_write(FILE *fp, const struct database *db)
{
    rewind(fp);

    if(fwrite(*db->db_data, sizeof(struct data), 1, fp) == -1) {
        return DATABASE_ERROR;
    }

    return 0;
}

struct database* database_read(FILE *fp, size_t db_rows, size_t db_data_size)
{
    struct database *db = NULL;
    size_t i = 0;

    db = malloc(sizeof(struct database));

    db->database_rows = db_rows;
    db->database_data_size = db_data_size;
    db->db_data = malloc(sizeof(struct data) * db_rows);

    for(i = 0; i < db_rows; i++) {
        db->db_data[i] = malloc(sizeof(struct data));
        db->db_data[i]->device = malloc(db_data_size);
        db->db_data[i]->resource = malloc(db_data_size);
    }

    rewind(fp);

    if(fread(*db->db_data, sizeof(struct data), 1, fp) == -1) {
        return NULL;
    }

    return db;
}

推荐答案

您似乎已经回答了自己的问题,fread和fwrite只是查看内存中的内容并将其放入文件中.如果您正在编写没有指针的东西(例如大数字数组),这将非常有用.它并非旨在编写带有指针的结构.

You seem to have answered your own question, fread and fwrite just look at what's in memory and put that in the file. This works great if you're writing things that don't have pointers (e.g. big arrays of numbers). It's not designed to write structs with pointers.

如果该文件具有格式,则需要按照格式说明进行操作.如果您正在编写一种格式,则应将每个成员一个接一个地写入文件.您将需要某种缓冲区才能读入(如果没有最大长度说明,则可能需要调整其大小).另外,您的database_write函数也需要进行很多更改.

If this file has a format, you need to do what the format says. If you're making up a format as you go, then you should write each member one by one into the file. You will need some sort of buffer to read into (you may need to resize this if you don't have a maximum length specification). Also, your database_write function will need to be changed quite a bit as well.

这篇关于fread'ing和fwrite'ing包含指针的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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