【发布时间】:2018-06-29 10:38:11
【问题描述】:
我正在尝试使用 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 );
}
}
【问题讨论】:
标签: c++ boost binary binaryfiles serialization