如何将动态结构保存到文件
我有这样的东西,实际上比这更复杂的结构:
typedef struct _sample {
unsigned char type;
char *name;
test *first;
} sample;
typedef struct _test {
test *prev;
test *next;
char *name;
int total;
test_2 **list;
} test;
typedef struct _test_2 {
char *name;
unsigned int blabla;
} test_2;
sample *sample_var;
我想将此结构备份到文件中并在恢复后恢复它。
我也尝试使用 fwrite(sample_var, sizeof(sample), 1, file_handle);
但真正的问题是 sizeof(sample)
返回错误的大小,而不是真正的变量大小。
有一种方法可以将其保存到文件中&在不知道大小的情况下恢复?
I have something like this, in fact more complex struct than this:
typedef struct _sample {
unsigned char type;
char *name;
test *first;
} sample;
typedef struct _test {
test *prev;
test *next;
char *name;
int total;
test_2 **list;
} test;
typedef struct _test_2 {
char *name;
unsigned int blabla;
} test_2;
sample *sample_var;
I want to backup this struct into a file and after restore it.
I also try with fwrite(sample_var, sizeof(sample), 1, file_handle);
but the real problem is sizeof(sample)
that return wrong size, not real variable size.
There is a way to save it into file & restore without knowing the size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在尝试序列化或编组结构。您不能只是
fwrite
数据(拥有指针是最明显的障碍)。与在文件中存储指针相比,sizeof
问题确实很小(指针在其起源的程序之外毫无意义)。您必须定义自己的序列化/反序列化函数。您可以使用自己的简单格式或使用 JSON, XML, XDR 或类似的东西。
就我个人而言,我会选择 JSON,因为无论如何它现在都很流行。
顺便说一句,这里有一个 C FAQ 模糊地链接到您自己的问题(尽管它讨论了互操作性问题)。
You are trying to serialize, or marshal the structure. You can't just
fwrite
the data (having pointers is the most obvious stopper). Thesizeof
problem is really minor when compared to storing pointers in a file (a pointer is meaningless outside the program where it originated).You will have to define your own serialization / deserialization functions. You could either use your own simple format or use JSON, XML, XDR or something like that.
Personally I would go with JSON, since it's all the rage these days anyway.
As an aside, here is a C FAQ vaguely linked to your own question (though it discusses interoperabillity issues).
没有简单的方法可以将这样的结构保存到文件中。例如,即使
sample.name
字段的大小为 4(取决于架构),而您可能想要保存的是sample.name
指向的内存内容。代码>.这是执行此类操作的示例代码。您将必须重复该过程才能保存整个结构。
这个想法是存储下一个结构的大小,然后写入内容。要从文件中获取信息,您需要读取大小,然后获取数据。
There is no easy approach to save such a structure into a file. For instance, even the
sample.name
field has a size of 4 (depending on architecture), while what you probably want to save is the content of the memory pointed bysample.name
.Here is a sample code that will do such a thing. You will have to duplicate the process to save the entire structure.
The idea is to store the size of the next structure and then writting the content. To get the information from the file, you read the size, and then get the data.
sizeof(sample) 并没有错误:它返回一个 char 的大小,后跟两个指针。如果需要保存这样的递归数据类型,则必须手动取消引用指针。
sizeof(sample) is not incorrect: it returns the size of a char followed by two pointers. If you need to save such a recursive data type, you have to manually follow dereference the pointers.
看起来您真正想要做的是存储结构及其指针所指的内容,而不是指针本身。
您将需要编写一些逻辑来确定所指向的数据的大小,并将该数据而不是指针写入文件。
It seems like what you really want to do is store the struct and what it's pointer's are referring to, not the pointers themselves.
You will need to write some logic the determine the size of the the data being pointed at, and write that data to the file instead of the pointers.