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

时间:2017-11-02 11:01:28

标签: c++ templates serialization boost stream

我已经将地图序列化了

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

我不明白为什么会这样。有人会非常友好地指出我正确的方向吗?

由于

2 个答案:

答案 0 :(得分:2)

您似乎从loadData复制saveData身体。您可以通过调用boost::filesystem::remove删除要尝试加载的文件。

答案 1 :(得分:0)

@VTT遇到了最大的错误。

这是我的免费代码审核:

  • 你不需要将boost :: filesystem改为std::remove(filename)
  • 而不是currentdir / filename您应该boost::filesystem::make_absolute
  • 已经是默认行为
  • 您应该使用native(),因为您可以传递路径
  • saveData的参数可以是const&
  • 不显式传递模板参数:函数调用执行模板参数演绎
  • 别介意你的意见非常多余。

<强> Live On Coliru

#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