简体   繁体   English

C是否有像python pickle这样的对象序列化对象?

[英]does C has anything like python pickle for object serialisation?

I'm wondering if C has anything similar to the python pickle module that can dump some structured data on disk and then load it back later. 我想知道C是否具有类似于python pickle模块的功能,该模块可以将一些结构化数据转储到磁盘上,然后稍后再加载回去。

I know that I can write my structure byte by byte to a file on disk and then read it back later, but with this approach there's still quite some work to do. 我知道我可以将我的结构逐字节写入磁盘上的文件中,然后稍后再读回,但是使用这种方法仍然有很多工作要做。 For example, if I have a single link list structure, I can traverse the list from head to tail and write each node's data on disk. 例如,如果我具有单个链接列表结构,则可以从头到尾遍历该列表并将每个节点的数据写入磁盘。 When I read the list back from the on-disk file, I have to reconstruct all links between each pair of nodes. 当我从磁盘文件中读回列表时,我必须重建每对节点之间的所有链接。

Please advise if there's an easier way. 请告知是否有更简单的方法。

Thanks heaps! 谢谢堆!

An emphatic NO on that one, I'm afraid. 恐怕对那一个很不满意。 C has basic file I/O. C具有基本文件I / O。 Any structuring of data is up to you. 数据的任何结构都取决于您。 Make up a format, dump it out, read it in. 编排格式,转储出来,读入。

There may be libraries which can do this, but by itself no C doesn't do this. 可能有一些库可以做到这一点,但是就其本身而言,没有C不能做到这一点。

The C library functions fread(3) and fwrite(3) will read and write 'elements of data', but that's pretty fanciful way of saying "the C library will do some multiplication and pread(2) or pwrite(2) calls behind the scenes to fill your array". C库函数fread(3)fwrite(3)将读取和写入“数据元素”,但这是一种非常pread(2)的说法,即“ C库将在后面进行一些乘法和pread(2)pwrite(2)调用场景来填补阵列”。

You can use them on struct s, but it is probably not a good idea: 可以struct上使用它们,但这可能不是一个好主意:

  • holes in the structs get written and read 读取和读取结构中的孔
  • you're baking in the endianness of your integers 您正在使用整数的字节序进行烘焙

While you can make your own format for writing objects, you might want to see if your application could use SQLite3 for on-disk storage of objects. 尽管您可以自己编写用于写入对象的格式,但是您可能希望查看您的应用程序是否可以使用SQLite3进行对象的磁盘存储。 It's well-debugged, and if your application fits its abilities well, it might be just the ticket. 它是经过充分调试的,并且如果您的应用程序很好地适合其功能,则可能只是问题所在。 (And a lot easier than writing all your own formatting code.) (比编写自己的所有格式代码容易得多。)

Boost具有序列化库 ,如果用“ C”表示“ C或C ++”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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