在纯C / C二进制序列++ [英] Binary serialization in pure C/C++

查看:271
本文介绍了在纯C / C二进制序列++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现二进制序列化我自己,而无需使用加速或其他任何第三方库。

I'd like to implement the binary serialization on my own, without using Boost or any other third-party library.

在C ++中实现这一目标是使用的ofstream ,然后发送一个二进制文件通过网络的方式simpliest。但有,我可以作为一个临时的缓冲区使用任何其他流类,以避免写文件到磁盘?

In C++ the simpliest way to achieve it is to use ofstream and then send a binary file over network. But is there any other stream class which I can use as a temporary buffer to avoid writing file to disk?

另外,我怎样才能实现在纯C?

Also, how can I achieve that in pure C?

推荐答案

坚持是很难的问题。它是不平凡的,即使对象序列到磁盘。说,例如,你有这样的一个是C的结构:

Persistence is hard issue. It is not trivial to even serialize an object to disk. Say that, for example, you have a structure like this one in C:

struct Person {
    char name[100];
    int year;
};

这是一个包含海基会结构,很可能在这才能真正应用于序列化的最简单方法。但是,你必须要面对以下问题:

This is a sef-contained structure, probably the simplest way in which serialization can really be applied. However, you'll have to face the following problems:


  1. 编译器的填充体系。完成内存中的结构,它占用的内存字的整数的方式是不标准的。

  1. The compiler's padding system. The way to complete a structure in memory so it occupies a whole number of words in memory is not standard.

的方式,操作系统和机器本身再以二进制形式presents数据。很显然,从一台机器这个重新presentation变化到另一个。

The way the operating system and the machine itself represents data in binary form. Obviously, this representation changes from one machine to another one.

的结论是,在相同的操作系统由相同的节目,即使在一个文件可能不与在相同的操作系统相同的程序兼容,因为也许两个节目用不同的C编译器编译。

The conclusion is that a file created even by the same program in the same operating system may not be compatible with the same program in the same operating system, because maybe both programs were compiled with different C compilers.

现在让我们来看看在C ++中的对象:

Now let's see an object in C++:

class Person {
public:
    // more things...

private:
    string name;
    Date * birth;
    Firm * firm;
};

现在同样的事情变得非常复杂。对象不再是独立的,你应该遵循的指针,以决定如何处理与每个对象(这就是所谓的三指针交叉混合和传递的持久性)。而你仍然有1)和2)的问题。

Now the very same thing has become really complex. The object is no more self-contained, you should follow the pointers in order to decide how to deal with each object (this is called 3. pointer swizzling and transitive persistence). And you still have 1) and 2) problems.

因此​​,让我们说你注重自我包含的对象,仍然需要点1安培的解决方案; 2.去的唯一方法是决定在任何一个)文本格式或b)字节code画幅的重新presentation。字节code格式可以通过在任何操作系统的任何程序,与任何C编译器编译可以理解,因为信息被读出和由字节写入字节。这是Java或C#序列的对象的方式。文本格式作为再presentation是一样有效字节code,但速度较慢。它的主要优点是,它可以由一个人正在以及计算机(结构化文本格式可以是XML)来理解。

So let's say that you focus on self-contained objects, and still need a solution for points 1 & 2. The only way to go is to decide a representation in either a) text format or b) bytecode format. Bytecode format can be understood by any program in any operating system, compiled with any C compiler, because the information is read and written byte by byte. This is the way that Java or C# serialize their objects. Text format as a representation is as valid as bytecode, though slower. Its main advantage is that it can be understood by a human being as well as the computer (a structured text format could be XML).

所以,为了您的序列化的自包含的对象,但是输出格式选择,你需要有(C或类++)是能够读取整数,字符,字符串等基本功能。当你有读/写对,每一个,你必须提供程序员可能创建自己的写/她的阅读对象对,用您阅读元素数据/写对。

So, in order to serialize your self-contained objects, however the output format chosen, you need to have basic functions (or classes in C++) that are able to read ints, chars, strings, and so on. When you have the write/read pairs for each one, you'll have to provide the programmer with the possibility to create her own write/read pairs for her objects, using you read/write pairs for elemental data.

我们这里所说的是一个完整的框架,像什么的Python与酱菜模块提供的东西。

We are talking here about a complete framework, something like what Python offers with its pickle module.

最后,能够缓存您的序列,而不是将其保存到磁盘的事实,就是至少你的问题。如果您使用的是基于文本的格式,或一个内存块,如果你正在使用的字节code您可以使用 ostringstream 类。

Finally, the fact of being able to cache your serialization instead of saving it to disk, is the least of your problems. You could use the ostringstream class if you are using a text-based format, or a memory block if you are using bytecode.

正如你所看到的,它不是一个简单的工作。
希望这有助于。

As you can see, it is not a simple job. Hope this helps.

这篇关于在纯C / C二进制序列++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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