开发者

Serializing to a binary archive using boost. Input Stream Error

开发者 https://www.devze.com 2023-04-06 08:51 出处:网络
I try to serialize to a binary archive then load this archive using the code show below. The issue I have is that when loading the file, I get an \"input stream error\".

I try to serialize to a binary archive then load this archive using the code show below. The issue I have is that when loading the file, I get an "input stream error".

    #include "project.h"

    // Std
    #include <fstream>

    // Boost
    #include <boost/archive/binary_oarchive.hpp>
    #include <boost/archive/binary_iarchive.hpp>
    #include <boost/algorithm/string.hpp>

    // Qt
    #include <QtGui/QMessageBox>
    #include <QFileInfo>
    #include <QDir>

    BOOST_CLASS_VERSION(Tools::CommentModel, 1)

    using namespace std;

    namespace Sawe {

    template<class Archive> 
    void runSerialization(Archive& ar, Project*& project, QString path)
    {

            const unsigned magicConst=65553;

        unsigned magic = magicConst;
        ar & BOOST_SERIALIZATION_NVP(magic);
        if (magic != magicConst)
            throw std::ios_base::failure("Wrong project type");

        ar & BOOST_SERIALIZATION_NVP(project);

    }

    bool Project::
            save()
    {
        if (project_filename_.empty()) {
            return saveAs();
        }


        try
        {
            std::ofstream ofs(project_filename_.c_str());
            assert(ofs.good());

            boost::archive::binary_oarchive xml(ofs);
            Project* p = this;
            runSerialization(xml, p, project_filename_.c_str());

            p->is_modified_ = false;
        }
        catch (const std::exception& x)
        {
            QString msg = "Error: " + QString::fromStdString(vartype(x)) +
                          "\nDetails: " + QString::fromLocal8Bit(x.what());
            QMessageBox::warning( 0, "Can't save file", msg );
            TaskInfo("======================\nCan't save file\n%s\n======================", msg.toStdString().c_str());
        }

        return true;
    }
    #endif


    pProject Project::
            openProject(std::string project_file)
    {
        std::ifstream ifs(project_file.c_str());

        boost::archive::binary_iarchive xml(ifs);
        Project* new_project = 0;
        runSerialization(xml, new_开发者_如何转开发project, project_file.c_str());

        new_project->project_filename_ = project_file;
        new_project->updateWindowTitle();
        new_project->is_modified_ = false;

        pProject project( new_project );

        return project;
    }
    }

Any idea ?

Thanks in advance for your help!

Cheers


I had the same problem, when i added the binary flag to the fstream ctors everything worked.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号