提高文件的系列化结束 [英] Boost serialization end of file

查看:139
本文介绍了提高文件的系列化结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我序列多个对象与升压二进制存档。
当回读那些对象 binary_iarchive ,有没有办法知道有多少对象是在存档还是一个简单的方法来检测归档的结束?

I serialize multiple objects into a binary archive with Boost. When reading back those objects from a binary_iarchive, is there a way to know how many objects are in the archive or simply a way to detect the end of the archive ?

我发现的唯一方法是使用一个try-catch来检测流异常。
先谢谢了。

The only way I found is to use a try-catch to detect the stream exception. Thanks in advance.

推荐答案

我能想到一些办法:


  1. 从存档序列化STL容器/(见的文档)。归档会自动跟踪多少对象有在容器中。

  1. Serialize STL containers to/from your archive (see documentation). The archive will automatically keep track of how many objects there are in the containers.

您的序列化对象之前序列化一个计数变量。当读回你的对象,你就会知道你事先预料多少个对象要读回。

Serialize a count variable before serializing your objects. When reading back your objects, you'll know beforehand how many objects you expect to read back.

我觉得这种做法有点严重,但我无论如何列出它们的完整性的缘故。 :-P你可以有象一种定点的指示的对象的列表的末尾的最后一个对象的行为。也许你可以在 isLast 的成员变量添加到对象。

I find this approach kinda gross, but I'm listing it anyway for the sake of completeness. :-P You could have the last object act like a kind of sentinel that indicates the end of the list of objects. Perhaps you could add an isLast member variable to the object.

这是不是很pretty,但你可以有存储对象的档案数一个单独的索引文件一起存档。

This is not very pretty, but you could have a separate "index file" alongside your archive that stores the number of objects in the archive.

使用底层流对象的 tellp 位置检测,如果你在文件的结尾:

Use the tellp position of the underlying stream object to detect if you're at the end of file:

例如:

std::streampos streamEnd = stream.seekp(0, std::ios_base::end).tellp();
stream.seekp(0, std::ios_base::beg);

while (stream.tellp() < streamEnd)
{
    // Deserialize objects
}

这可能不适用于XML存档工作。

This might not work with XML archives.

这篇关于提高文件的系列化结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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