简体   繁体   中英

C data structure to disk

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

You need to serialize it, ie 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.

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 CI/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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM