这是我尝试过的方法,但是我得到了 Error: Unknown exception
thrown
try{//load
std::ifstream stream(arch_name.c_str());
std::cout << ">> " << "Started deserializing " << arch_name << std::endl;
boost::archive::binary_iarchive arc(stream);
arc & c & matrix;
stream.close();
std::cout << ">> " << "Finished deserializing" << std::endl;
}catch(std::exception e){
std::cout << "Error: " << e.what() << std::endl;
return 1;
}
这在带有 gcc 的 Linux 中运行良好。我在 Windows 中使用 Visual Studio。
编辑
回溯显示 basic_binary_iprimitive.hpp
正在抛出异常
template<class Archive, class Elem, class Tr>
inline void basic_binary_iprimitive<Archive, Elem, Tr>::load_binary( void *address, std::size_t count){
//.....
if(scount != s)
boost::serialization::throw_exception(
archive_exception(archive_exception::input_stream_error)
);
编辑
我将 catch
black 更改为 catch(boost::archive::archive_exception e)
并打印出来。 输入流错误
boost archive exception 中的 virtual what 有没有遗漏?
最佳答案
我遇到这个问题是因为我遇到了同样的问题 - Windows 上的 boost 序列化异常。我在 Greg 的评论中找到了解决方案:在创建数据流时定义 i/o 模式。
我改变了这个:
std::ofstream ofs(filename);
boost::archive::binary_oarchive ar(ofs);
对此:
std::ofstream ofs(filename, std::ios::binary);
boost::archive::binary_oarchive ar(ofs);
然后在反序列化的时候就没有再出现异常了!
我认为这个问题需要一个实际的答案(而不仅仅是评论),所以我自己添加了它,但所有功劳归于 Greg。
关于c++ - Windows 中 boost::serialization 的未知异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11924126/