提升文件末尾序列化
我使用 Boost 将多个对象序列化为二进制存档。 当从 binary_iarchive
读回这些对象时,有没有办法知道存档中有多少对象,或者只是检测存档结尾的方法?
我发现的唯一方法是使用 try-catch 来检测流异常。 提前致谢。
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 ?
The only way I found is to use a try-catch to detect the stream exception.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我可以想到多种方法:
将 STL 容器序列化到存档或从存档中序列化(请参阅 文档)。存档将自动跟踪容器中有多少对象。
在序列化对象之前序列化计数变量。读回对象时,您将事先知道希望读回多少个对象。
您可以让最后一个对象具有特殊值,该值充当一种标记,指示对象列表的末尾。也许您可以向对象添加一个
isLast
成员函数。这不是很漂亮,但是您可以在存档旁边有一个单独的“索引文件”,用于存储存档中的对象数量。
使用基础流对象的
tellp
位置来检测是否位于文件末尾:示例(只是一个草图,未经测试):
这可能不适用于 XML 存档。
I can think of a number of approaches:
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.
You could have the last object have a special value that acts as a kind of sentinel that indicates the end of the list of objects. Perhaps you could add an
isLast
member function to the object.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.
Use the
tellp
position of the underlying stream object to detect if you're at the end of file:Example (just a sketch, not tested):
This might not work with XML archives.
当您开始序列化时,您是否拥有所有对象?如果不是,那么您就是在“滥用”boost 序列化 - 它不应该以这种方式使用。但是,我就是这样使用它的,使用
try catch
来查找文件的末尾,它对我有用。只需将其隐藏在实现中的某个地方即可。但请注意,如果以这种方式使用它,您需要要么不序列化指针,要么禁用指针跟踪。如果您已经拥有所有对象,请参阅埃米尔的回答。它们都是有效的方法。
Do you have all your objects when you begin serializing? If not, you are "abusing" boost serialization - it is not meant to be used that way. However, I am using it that way, using
try catch
to find the end of the file, and it works for me. Just hide it away somewhere in the implementation. Beware though, if using it this way, you need to either not serialize pointers, or disable pointer tracking.If you do have all the objects already, see Emile's answer. They are all valid approaches.
您刚刚从文件中读取了一个字节。
如果没有到达末尾,
则回写一个字节。
you just read a byte from the file.
If you do not reach the end,
backword a byte then.
从存档中读取数据时使用 zip,并且
filtering_streambuf
有这样的方法,因此我检查存档的末尾,因为
它无助于了解存档中最后有多少对象,但有助于检测他们的尽头
(我仅在单元测试中使用 eof 测试来序列化/反序列化我的类/结构 - 以确保我正在阅读我正在编写的所有内容)
using zip when reading data from the archives, and
filtering_streambuf
have such method asso i check the end of archive as
It is not helping to know how many objects are last in the archive, but helping to detect the end of them
(i'm using eof test only in the unit test for serialization/unserialization my classes/structures - to make sure that i'm reading all what i'm writing)