我在这里的情况有点复杂。我想将一个向量结构保存到一个文件中,并在一段时间后读取它。但问题在于阅读。我不知道如何将所有向量从保存的文件中填充到结构中。
struct TDNATable{
std::vector<String>GenomOne;
std::vector<String>GenomeL;
std::vector<String>GenomeER;
std::vector<String>GenomeRET;
std::vector<String>GenomeSEL;
};
std::vector<TDNATable> DnaTbl;
//PSEUDO CODE:
//For example simple writing could be
ofstream file("C:\\Users\\User11\\Desktop\\SNF_TBL.INI", ios::binary);
file.write((char*)&DnaTbl, sizeof(DnaTbl));
//Problem comes with reading
// impl
ifstream file("C:\\Users\\User11\\Desktop\\SNF_TBL.INI",
std::ifstream::binary);
// get pointer to associated buffer object
std::filebuf* pbuf = file.rdbuf();
// get file size using buffer's members
std::size_t size = pbuf->pubseekoff(0, file.end, file.in);
pbuf->pubseekpos(0, file.in);
// allocate memory to contain file data
char* buffer = new char[size];
// get file data
pbuf->sgetn(buffer, size);
file.close();
for (int i = 0; i < SnfTbl.size(); i++) {
//Back inserter can be used only with 1D vector
std::copy(buffer, buffer +sizeof(buffer),
std::back_inserter(SnfTbl[i].GenomeL);
std::copy(buffer, buffer +sizeof(buffer),
std::back_inserter(SnfTbl[i].GenomeER));
}
RefreshDFSGrid();
delete[]buffer;
file.close();
我尝试使用 boost/serialize 但没有成功。
您知道如何以优雅的方式保存/加载此 ds 吗?谢谢!