c - 读取和写入内存数据

标签 c protocol-buffers

在下面的示例中,我采用一个在内存中占用 32 个字节的结构,并将其写入文件并将其读回 - 即,将数据序列化为二进制格式:

#include <stdio.h>

typedef struct _Person {
    char   name[20];
    int    age;
    double weight;
} Person;

int main(void)
{

    Person tom = (Person) {.name="Tom", .age=20, .weight=125.0};

    // write the struct to a binary file
    FILE *fout = fopen("person.b", "wb");
    fwrite(&tom, sizeof tom, 1, fout);
    fclose(fout);

    // read the binary data and set the person to that
    Person unknown;
    FILE *fin = fopen("person.b", "rb");
    fread(&unknown, sizeof unknown, 1, fin);
    fclose(fin);

    // confirm all looks ok
    printf("{name=%s, age=%d, weight=%f}", unknown.name, unknown.age, unknown.weight);

}

但请注意,这些都是堆栈上的值,不涉及指针/间接。例如,当可能涉及多个指针、多个变量可能指向同一内存位置等时,数据如何序列化到文件中。这实际上是 Protocol Buffer 所做的吗?

最佳答案

好吧,你想要一个二进制文件。我很久以前就这样做过。没关系。当您转移到另一个平台或位时,它就会崩溃。我正在用旧的方式教学,因为这是一个很好的起点。更新的方法现在很流行,因为它们在改变平台或位数时仍然有效。

将记录写入文件时,我会使用如下结构:

typedef struct _Person {
    char   name[20];
    int    age;
    double weight;
} Person;

typedef struct _Thing {
    char name[20];
};

typedef struct _Owner {
    int personId;
    int thingId;
} Owner;

查看Owner 结构如何具有 Id 成员。这些只是其他结构数组的索引。

这些可以一个接一个地写到一个文件中,通常以一个直接写入的整数为前缀,表示每种类型有多少条记录。读者只需使用足够大的 malloc 来分配一个结构数组来容纳它们。当我们在内存中添加更多项目时,我们使用 realloc 调整数组的大小。我们还可以(并且应该)标记删除(例如将名称的第一个字符设置为 0)并稍后重新使用该记录。

作者看起来像这样:

void writeall(FILE *h, Person *allPeople, int nPeople, Thing *allThings, int nThings, Owner *allOwners, int nOwners)
{
    // Error checking omitted for brevity
    fwrite(&nPeople, sizeof(nPeople), 1, h);
    fwrite(allPeople, sizeof(*allPeople), nPeople, h);
    fwrite(&nThings, sizeof(nThings), 1, h);
    fwrite(allThings, sizeof(*allThings), nThings, h);
    fwrite(&nOwners, sizeof(nOwners), 1, h);
    fwrite(allOwners, sizeof(*allOwners), nOwners, h);
}

阅读器依次看起来像这样:

int writeall(FILE *h, Person **allPeople, int *nPeople, int *aPeople, Thing **allThings, int *nThings, int *aThings, Owner **allOwners, int *nOwners, int *aOwners)
{
    *aPeople = 0; // Don't crash on bad read
    *aThigns = 0;
    *aOwners = 0;
    *allPeople = NULL;
    *allThings = NULL;
    *allOwners = NULL;

    if (1 != fread(nPeople, sizeof(*nPeople), 1, h)) return 0;
    *allPeople = malloc(sizeof(**allPeople) * *nPeople);
    if (!allPeople) return 0; // OOM
    *aPeople = *nPeople;
    if (*nPeople != fread(*allPeople, sizeof(**allPeople), nPeople, h)) return 0;

    if (1 != fread(nThings, sizeof(*nThings), 1, h)) return 0;
    *allThings = malloc(sizeof(**allThings) * *nThings);
    if (!allThings) return 0; // OOM
    *aThings = *nThings;
    if (*nThings != fread(*allThings, sizeof(**allThings), nThings, h)) return 0;

    if (1 != fread(nOwners, sizeof(*nOwners), 1, h)) return 0;
    *allOwners = malloc(sizeof(**allOwners) * *nOwners);
    if (!allOwners) return 0; // OOM
    *aOwners = *nOwners;
    if (*nOwners != fread(*allOwners, sizeof(**allOwners), nOwners, h)) return 0;

    return 1;
}

有一种旧技术可以将堆区域直接写入磁盘并再次读回。我建议永远不要使用它,并且永远不要将指针存储在磁盘上。这就是安全噩梦。

当内存很便宜时,我会讨论如何使用 block 分配和链接 block 来部分动态更新磁盘上的记录;但现在对于你在这个级别遇到的问题,我说不要打扰,只需将整个内容读入 RAM 并再次写回即可。最终你将学习数据库,它会为你处理这些事情。

关于c - 读取和写入内存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66235920/

相关文章:

c - 当添加一个整数时,仅记录第一个数字

c - 如何中断pselect?

ruby-on-rails - 任何使用 ruby​​-protobuf 和 A​​ctiveRecord 的人

json - "google/protobuf/struct.proto"是通过 GRPC 发送动态 JSON 的最佳方式吗?

c++ - 将 Keras 模型转换为 TensorFlow protobuf

c - K&R 代码在 VS2012 中不起作用

c - 无法通过 unix 系统调用使用 c 写入文件

c - 如何读取带有空格的输入并将输入排序到 2 个数组?

protocol-buffers - 为什么 protobuf FieldMask 使用字段名称而不是字段编号?

java - Java 中的 Protocol Buffer : can we handle primitive arrays efficiently?