2

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

2
  • 1
    Using your debugger, try stepping through your code line-by-line until you trigger the exception. That'll help narrow down the cause of your error. You can also set the debugger to halt when an exception is thrown. If you have a debug version of the serialization library, you'll see which line is throwing the exception, as well as a stack trace showing the program "context". Commented Sep 19, 2011 at 14:30
  • Are you aware that Qt has its own serialization framework? doc.qt.nokia.com/stable/datastreamformat.html Commented Sep 19, 2011 at 14:32

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.