提升反序列化问题:运行时输入流错误 (c++)
Boost deserialisation issue : Input Stream Error at runtime (c++)
我已经很好地序列化了一张地图
std::map<std::string, std::string> userandPass;
saveData< std::map<std::string, std::string> >("userandPassBackup.txt", userandPass); // have removed &. why is this needed. i want to pass by reference
使用该函数
template <typename SaveClass>
void saveData(const std::string filename, SaveClass& c)
{
// File to be written to
boost::filesystem::remove(boost::filesystem::current_path() /
filename);
boost::filesystem::path myFile = boost::filesystem::current_path() /
filename;
// Declare an output file stream ofs that writes serialises to our
//backup file.
boost::filesystem::ofstream ofs(myFile.native());
// Declare archive ta that will use our output file stream
boost::archive::text_oarchive ta(ofs);
// Write to file
ta << c;
// How many records have been archived?
std::cout << c.size() << " records from have been successfully backed
up to " << myFile << "n";
}
但是,反序列化(加载)失败,使用:
loadData< std::map<std::string, std::string> >("userandPassBackup.txt", userandPass);
其中函数为:
template <typename LoadClass>
void loadData(const std::string filename, LoadClass& c)
{
// File to be written to
boost::filesystem::remove(boost::filesystem::current_path() /
filename);
boost::filesystem::path myFile = boost::filesystem::current_path() /
filename;
// Declare an output file stream ofs that writes serialises to our
//backup file.
boost::filesystem::ifstream ifs(myFile.native());
// Declare archive ta that will use our output file stream
boost::archive::text_iarchive ta(ifs);
// Write to file
ta >> c;
// How many records have been archived?
std::cout << c.size() << " records from have been successfully backed
up to " << myFile << "n";
}
我的项目编译,但是当我运行它时,我收到以下有关输入流的错误:
libc++abi.dylib:终止时出现 boost::archive::archive_exception 类型的未捕获异常:输入流错误中止陷阱:6
我不明白为什么会发生这种情况。有人会好心地为我指出正确的方向吗?
谢谢
好像你从saveData
复制粘贴了loadData
身体.首先,通过调用 boost::filesystem::remove
删除尝试加载的文件。
@VTT遇到了最大的错误。
这是我的免费代码评论:
- 你不需要 boost::filesystem 来
std::remove(filename)
- 而不是
currentdir / filename
你应该做boost::filesystem::make_absolute
- 这已经是默认行为
- 您不应该使用
native()
因为您可以传递路径 saveData
的论据可以const&
- 不要显式传递模板参数:函数调用执行模板参数推导。
- 没关系,您的评论非常多余。
住在科里鲁
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <cstdio>
#include <iostream>
#include <fstream>
template <typename SaveClass>
void saveData(const std::string filename, SaveClass const& c)
{
std::remove(filename.c_str());
std::ofstream ofs(filename);
boost::archive::text_oarchive ta(ofs);
ta << c;
}
template <typename LoadClass>
void loadData(const std::string filename, LoadClass& c)
{
std::ifstream ifs(filename);
boost::archive::text_iarchive ta(ifs);
ta >> c;
}
int main() {
std::string filename = "userandPassBackup.txt";
{
std::map<std::string, std::string> userandPass {
{ "John", "pa$$w0rd" },
{ "Jane", "welcome01" } };
saveData(filename, userandPass);
std::cout << userandPass.size() << " records from have been successfully backed up to " << filename << "n";
}
{
std::map<std::string, std::string> userandPass;
loadData(filename, userandPass);
std::cout << userandPass.size() << " records from have been successfully restored from " << filename << "n";
}
}
指纹
2 records from have been successfully backed up to userandPassBackup.txt
2 records from have been successfully restored from userandPassBackup.txt
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'