使用提升序列化读取具有批大小的二进制文件
Reading binary file with batch size using boost serialization
我正在尝试使用 boost 的二进制序列化程序序列化该类,并以批量大小加载二进制数据。
我能够获取 10 条记录中的前 5 条记录,当我尝试通过重新打开文件并将文件读取指针重新设置为前一条记录来读取另外 5 条记录时,该过程崩溃。我很确定,我在这里缺少一些东西来正确重新设置文件读取指针。
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/string.hpp>
#include <fstream>
#include <iostream>
using namespace std;
using namespace boost::archive;
class logEntry {
private:
size_t m_txID;
string m_jsonStr;
friend class boost::serialization::access;
template <typename Archive>
friend void serialize( Archive &ar, logEntry &l, const unsigned int version );
public:
logEntry() {
m_txID = 0;
m_jsonStr = "";
}
logEntry( size_t id, const string &val ) {
m_txID = id;
m_jsonStr = val;
}
string getJsonValue() {
return m_jsonStr;
}
size_t getTxId() {
return m_txID;
}
};
template <typename Archive>
void serialize( Archive &ar, logEntry &l, const unsigned int version ) {
ar &l.m_txID;
ar &l.m_jsonStr;
}
size_t prevReadPos = 0;
void save() {
ofstream file{"/tmp/test.bin", ios::binary | ios::trunc};
binary_oarchive oa{file};
// Save 10 records
for ( int i = 0; i < 10; i++ )
oa << logEntry( i, "{Some Json String}" );
file.flush();
file.close();
}
// Load data batch wise
void load( size_t bsize ) {
ifstream file{"/tmp/test.bin", ios::binary};
binary_iarchive ia{file};
// Record file length
size_t fileEnd;
size_t beg = file.tellg();
file.seekg( 0, ios::end );
fileEnd = file.tellg();
// Reset the file pointer to the beginning of the file OR to the previous read position
if ( prevReadPos == 0 )
file.seekg( beg, ios::beg );
else
** // THIS IS THE PLACE I AM RESTORING THE PREVIOUS READ POSITION, WHICH IS CAUSING THE PROCESS
// TO CRASH.**
file.seekg( prevReadPos, ios::beg );
// Load records batch wise until we reach the end of the file
logEntry l;
for ( size_t i = 0; i < bsize && file.tellg() < fileEnd; i++ ) {
ia >> l;
}
prevReadPos = file.tellg();
file.close();
}
int main() {
save();
while ( 1 ) {
load( 5 );
sleep( 5 );
}
}
您使用了
错误的提升序列化。
档案不是"随机访问"。它们是完整的存储格式,带有标头和(可能(关闭序列。
如果您创建一个包含 10 条记录的存档,您将 [必须] 读取整个存档。当然,你可以中途停下来,因为,你知道,你还不需要阅读其余的。但是,当然,您不能指望稍后再阅读其余内容,就好像那时开始了一个全新的存档一样,因为没有。结果是未定义的行为。
另请参阅有关将多个存档合并到一个文件中的信息(除非使用自己的描述格式,否则您不想这样做(:
- 存档不是流
- 输出比多态文本存档更多的东西
相关文章:
- 正在读取二进制文件(is_open)
- 在C++中将类(带有Vector成员)保存为二进制文件
- 如何从二进制文件中读取字符串
- 保存/加载大量短数组到二进制文件
- 从二进制文件中读取整数数组
- Android 在编译二进制文件时重建静态库
- 在 C++ 中将双精度变量写入二进制文件
- clang 的 libFuzzer 可以在同一二进制文件中测试超过 1 个 API 吗?
- C++:实际上不是从二进制文件中读取
- 如何从二进制文件中的给定符号中获取调用程序图
- 将内部带有矢量的结构保存/读取到二进制文件中
- 编译多个C++文件.调用二进制文件以运行代码
- 如何使用位字段将数据从二进制文件复制到结构中?
- uint8_t同一二进制文件的不同十进制值
- C++单个生成文件多个二进制文件
- 尝试将数字写入二进制文件时引发异常
- C++中读/写二进制文件
- 如何忽略某些二进制文件的执行?
- 对在不同二进制文件中创建的对象文件的依赖关系
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作