提升反序列化问题:运行时输入流错误 (c++)

Boost deserialisation issue : Input Stream Error at runtime (c++)

本文关键字:错误 c++ 输入流 运行时 反序列化 问题      更新时间:2023-10-16

我已经很好地序列化了一张地图

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