C数据结构到磁盘

发布于 2024-09-19 15:53:39 字数 31 浏览 1 评论 0原文

如何用 C 语言将内存中的树数据结构复制到磁盘?

How can I make a copy of a tree data structure in memory to disk in C programming language?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻找一个思念的角度 2024-09-26 15:53:40

您需要将其序列化,即找到一种方法来串行遍历它,包括所有节点。这些通常称为遍历方法

然后找出一种方法来存储每个节点的表示以及对其他节点的引用,以便可以再次加载它们。

隐式表示引用的一种方法是通过像 XML 那样的嵌套。

You need to serialize it, i.e. figure out a way to go through it serially that includes all nodes. These are often called traversal methods.

Then figure out a way to store the representation of each node, together with references to other nodes, so that it can all be loaded in again.

One way of representing the references is implicitly, by nesting like XML does.

無心 2024-09-26 15:53:40

这里的基本部分是:

  • C 文件 I/O 例程是 fopen 、fwrite、fprintf 等。
  • 将指针复制到磁盘是没有用的,因为下次运行所有这些指针值时将是垃圾。因此,您需要一些替代指针的方法,以某种方式将磁盘记录相互引用。一种明智的替代方案是文件索引(CI/O 例程使用的类型,例如 fseek ftell)。

这应该是您完成这项工作所需的所有信息。

或者,如果您使用基于数组的树(使用数组索引而不是指针,或者使用它们在数组中的位置暗示的链接),您可以只保存并加载整个 shebang,而不需要任何进一步的逻辑。

The basic pieces here are:

  • The C file I/O routines are fopen, fwrite, fprintf, etc.
  • Copying pointers to disk is useless, since the next time you run all those pointer values will be crap. So you'll need some alternative to pointers that still somehow refers disk records to each other. One sensible alternative would be file indexes (the kind used by your C I/O routines like fseek and ftell).

That should be about all the info you need to do the job.

Alternatively, if you use an array-based tree (with array indexes instead of pointers, or with the links implied by their position in the array) you could just save and load the whole shebang without any further logic required.

相思故 2024-09-26 15:53:40

提出一个序列化(和反序列化)函数。然后运行它并将输出发送到文件。

Come up with a serialization (and deserialization) function. Then run it and send the output to a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文