大尺寸向量的 C++ 谷物反序列化问题
C++ cereal de-serialization trouble with large size vector
我希望用cereal,C++序列化库来序列化大型向量。
但是,如果尝试这样做,则会抛出异常 "Failed to read " + std::to_string(size) + " bytes from input stream! Read " + std::to_string(readSize)" .
有人知道解决这个问题的好方法吗?
我正在使用 VisualStudio 2017。
源码如下所示
#include <iostream>
#include <fstream>
#include "include\cereal\cereal.hpp"
#include "include\cereal\archives\binary.hpp"
#include "include\cereal\types\vector.hpp"
#include "include\cereal\types\string.hpp"
void show(std::vector<int> v) {
for (auto i : v)std::cout << i << ",";
std::cout << std::endl;
}
int main(void) {
const std::string file_name = "out.cereal";
{
std::vector<int> src;
// const int STOP = 10; //OK
const int STOP = 1000; // NG
for (int i = 0; i < STOP; i++)src.push_back(i);
std::cout << "src:" << std::endl;
show(src);
std::ofstream ofs(file_name, std::ios::binary);
cereal::BinaryOutputArchive archive(ofs);
archive(src);
}
{
std::vector<int> dst;
std::fstream fs(file_name);
cereal::BinaryInputArchive iarchive(fs);
iarchive(dst);
std::cout << "dst:" << std::endl;
show(dst);
}
#ifdef _MSC_VER
system("pause");
#endif
return 0;
}
你的代码在 Linux 中对我来说工作正常,所以我认为这与 Windows 上文本和二进制处理之间的差异有关。检查您在构建输入流时是否传递了 std::ios::binary
。还将其构造为 std::ifstream
而不仅仅是 std::fstream
.
我认为这可能与 Windows 期望(或添加)Unicode 字节顺序标记有关,这会混淆序列化程序。
我希望用cereal,C++序列化库来序列化大型向量。
但是,如果尝试这样做,则会抛出异常 "Failed to read " + std::to_string(size) + " bytes from input stream! Read " + std::to_string(readSize)" .
有人知道解决这个问题的好方法吗? 我正在使用 VisualStudio 2017。
源码如下所示
#include <iostream>
#include <fstream>
#include "include\cereal\cereal.hpp"
#include "include\cereal\archives\binary.hpp"
#include "include\cereal\types\vector.hpp"
#include "include\cereal\types\string.hpp"
void show(std::vector<int> v) {
for (auto i : v)std::cout << i << ",";
std::cout << std::endl;
}
int main(void) {
const std::string file_name = "out.cereal";
{
std::vector<int> src;
// const int STOP = 10; //OK
const int STOP = 1000; // NG
for (int i = 0; i < STOP; i++)src.push_back(i);
std::cout << "src:" << std::endl;
show(src);
std::ofstream ofs(file_name, std::ios::binary);
cereal::BinaryOutputArchive archive(ofs);
archive(src);
}
{
std::vector<int> dst;
std::fstream fs(file_name);
cereal::BinaryInputArchive iarchive(fs);
iarchive(dst);
std::cout << "dst:" << std::endl;
show(dst);
}
#ifdef _MSC_VER
system("pause");
#endif
return 0;
}
你的代码在 Linux 中对我来说工作正常,所以我认为这与 Windows 上文本和二进制处理之间的差异有关。检查您在构建输入流时是否传递了 std::ios::binary
。还将其构造为 std::ifstream
而不仅仅是 std::fstream
.
我认为这可能与 Windows 期望(或添加)Unicode 字节顺序标记有关,这会混淆序列化程序。