gpt4 book ai didi

c - 读取和写入内存数据

转载 作者:行者123 更新时间:2023-12-04 08:05:36 24 4
gpt4 key购买 nike

在下面的示例中,我将一个在内存中占用 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 调整数组大小.我们可以(也应该)标记为删除(比如将 name 的第一个字符设置为 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;
}
有一种古老的技术可以将堆区域直接写入磁盘并再次读取它。我建议永远不要使用它,也不要在磁盘上存储指针。那就是安全噩梦。
当内存便宜时,我会讨论如何使用块分配和链接块来部分动态更新磁盘记录;但是现在对于您将在此级别遇到的问题,我说不要打扰,只需将整个内容读入 RAM 并再次写回即可。最终你将学习数据库,它为你处理这些事情。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com