写动态分配结构到文件 [英] Write dynamically allocated structure to file

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

问题描述

假设我们有以下结构:

    struct Something {
        int i;
    };

如果我想在一个文件中写入这种类型(动态分配),我做的任何数据:

If I want to write in a file any data of this type(dynamically allocated), I do this:

struct Something *object = malloc(sizeof(struct Something));
object->i = 0; // set member some value
FILE *file = fopen("output_file", "wb");
fwrite(object, sizeof(struct Something), 1 file);
fclose(file);

现在,我的问题:

我们如何做什么包含指针的结构?我测试用同样的方法,它工作得很好,数据可能被读取,但我想知道是否有什么风险吗?

How we do this with a structure what contains pointers? I tested using same method, it worked fine, data could been read, but I want to know if there are any risks?

推荐答案

您想要什么叫系列化。还请参阅 XDR (便携式二进制数据格式)及 libs11n (一个C ++二进制序列化库);你经常关心数据的便携性:能够读取一些数据的不同的的计算机

What you want is called serialization. See also XDR (a portable binary data format) & libs11n (a C++ binary serialization library); you often care about data portability: being able to read the data on some different computer.

序列化是指转换一些复杂的数据结构(如列表,一,矢量甚至是你的的东西 ...)成(串行)字节流(例如,一个文件,网络连接等),和向后。圆形的数据结构或共享的子组件处理可能会非常棘手。

"serialization" means to "convert" some complex data structure (e.g. a list, a tree, a vector or even your Something...) into a (serial) byte stream (e.g. a file, a network connection, etc...), and backwards. Dealing with circular data structures or shared sub-components may be tricky.

您不想写一个文件中的原始指针(但你可以),因为写的地址可能不会在你的程序(因为如的 ASLR ),即当你将再次读取数据。

You don't want to write raw pointers inside a file (but you could), because the written address probably won't make any sense at the next execution of your program (e.g. because of ASLR), i.e. when you'll read the data again.

也阅读有关应用程序检查点并的持久

有关实际的理由(尤其是缓解调试和弹性WRT小软件进化)通常最好使用一些的文本数据格式(例如像 JSON 或的 YAML )来存储这样的持久性数据。

For pragmatic reasons (notably ease of debugging and resilience w.r.t. small software evolution) it is often better to use some textual data format (like e.g. JSON or Yaml) to store such persistent data.

您可能也有兴趣在数据库的。先看看 sqlite的,同时也为数据库管理系统(关系-or的 SQL based-的像 PostgreSQL的,< A HREF =htt​​ps://en.wikipedia.org/wiki/NoSQL相对=nofollow> NoSQL的的像如的 MongoDB的

You might also be interested in databases. Look first into sqlite, and also into DBMS ("relational" -or SQL based- ones like PostGreSQL, NoSQL ones like e.g. MongoDB)

问题不是写一个动态分配的结构(因为你想大多写的数据内容,而不是指针,所以它是同为 FWRITE A 的malloc -ed 结构或本地分配的),它被序列化这使用大量奇怪的内部指针的复杂数据结构!

The issue is not writing a single dynamically allocated struct (since you want mostly to write the data content, not the pointer, so it is the same to fwrite a malloc-ed struct or a locally allocated one), it is to serialize complex data structures which use lots of weird internal pointers!

注意,复制垃圾收集类似序列算法使用的算法(因为这两个需要扫描一个复杂的图表引用)。

Notice that copying garbage collectors use algorithms similar to serialization algorithms (since both need to scan a complex graph of references).

此外,在今天的计算机,磁盘 - 或网络 - IO是很多(例如一百万次),比CPU慢,所以是有意义的写入文件之前做一些显著计算。

Also, on today's computers, disk -or network- IO is a lot (e.g. a million times) slower than the CPU, so it makes sense to do some significant computation before writing files.

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

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