Boost序列化在一定文件大小后抛出“输入流错误”

Posted

技术标签:

【中文标题】Boost序列化在一定文件大小后抛出“输入流错误”【英文标题】:Boost serialization throws "input stream error" after a certain file size 【发布时间】:2013-03-09 19:09:41 【问题描述】:

我正在使用 Boost::serialization 读取/写入包含 3d 模型的文件。

在文件达到一定大小(大约 8-12kb+)之后,每次我尝试读取文件时,序列化都会引发“输入流错误”异常。直到那时它每次都可以正常工作 - 例如我可以成功读/写一个 1kb 模型 12 次,然后在第 13 次及以后它会抛出异常!

这是序列化/反序列化的代码:

    JonsPackagePtr ReadJonsPkg(const std::string& jonsPkgName)
    
        std::ifstream jonsPkgStream(jonsPkgName.c_str(), std::fstream::binary || std::fstream::in);
        JonsPackagePtr pkg(new JonsPackage());   // "JonsPackagePtr" is a boost_shared_ptr typedef

        if (jonsPkgStream && jonsPkgStream.good() && jonsPkgStream.is_open())
        
            boost::archive::binary_iarchive iar(jonsPkgStream);

            iar >> (*pkg.get());
        

        return pkg;
    

    bool WriteJonsPkg(const std::string& jonsPkgName, const JonsPackagePtr pkg)
    
        std::ofstream outStream(jonsPkgName.c_str(), std::fstream::out | std::fstream::binary);
        bool ret = false;

        if (outStream.is_open())
        
            boost::archive::binary_oarchive oar(outStream);
            oar << (*pkg.get());

            ret = true;
        

        return ret;
    

这是我存档的内容:

/* PackageHeader definition */
    struct PackageHeader
    
        std::string mSignature;
        uint8_t mMajorVersion;
        uint8_t mMinorVersion;


        PackageHeader();
    ;

    /* PackageMesh definition */
    struct PackageMesh
    
        std::vector<float> mVertexData;
        std::vector<uint32_t> mIndiceData;


        PackageMesh();
    ;

    /* PackageModel definition */
    struct PackageModel
    
        std::string mName;
        std::vector<PackageModel> mChildren;
        std::vector<PackageMesh> mMeshes;
        Mat4 mTransform;


        PackageModel();
    ;

    /* JonsPackage definition */
    struct JonsPackage
    
        PackageHeader mHeader;
        std::vector<PackageModel> mModels;


        JonsPackage();
    ;


    typedef boost::shared_ptr<JonsPackage> JonsPackagePtr;
    JonsPackagePtr ReadJonsPkg(const std::string& jonsPkgName);
    bool WriteJonsPkg(const std::string& jonsPkgName, const JonsPackagePtr pkg);


    /* PackageHeader inlines */
    inline PackageHeader::PackageHeader() : mSignature("jons"), mMajorVersion(LatestMajorVersion), mMinorVersion(LatestMinorVersion)
    
    

    /* PackageModel inlines */
    inline PackageModel::PackageModel() : mName(""), mTransform(1.0f)
    
    

    /* PackageMesh inlines */
    inline PackageMesh::PackageMesh()
    
    

    /* JonsPackage inlines */
    inline JonsPackage::JonsPackage()
    
    

最后是我的非侵入式序列化定义:

namespace boost

    namespace serialization
    
        template<class Archive>
        void serialize(Archive & ar, JonsEngine::PackageHeader& header, const unsigned int version)
        
            ar & header.mMajorVersion;
            ar & header.mMinorVersion;
            ar & header.mSignature;
        

        template<class Archive>
        void serialize(Archive & ar, JonsEngine::PackageModel& model, const unsigned int version)
        
            ar & model.mName;
            ar & model.mChildren;
            ar & model.mMeshes;
            ar & model.mTransform;
        

        template<class Archive>
        void serialize(Archive & ar, JonsEngine::PackageMesh& mesh, const unsigned int version)
        
            ar & mesh.mVertexData;
            ar & mesh.mIndiceData;
        

        template<class Archive>
        void serialize(Archive & ar, JonsEngine::JonsPackage& pkg, const unsigned int version)
        
            ar & pkg.mHeader;
            ar & pkg.mModels;
        

        template<class Archive>
        void serialize(Archive & ar, glm::detail::tmat4x4<glm::mediump_float> transform, const unsigned int version)
        
            ar & transform[0];
            ar & transform[1];
            ar & transform[2];
            ar & transform[3];
        

        template<class Archive>
        void serialize(Archive & ar, glm::detail::tvec4<glm::mediump_float> vec, const unsigned int version)
        
            ar & vec.x;
            ar & vec.y;
            ar & vec.z;
            ar & vec.w;
        
     // namespace serialization
 // namespace boost

正如我上面提到的,只有在我尝试读取某个文件大小(8-12kb+)之后,它才会开始抛出异常。到底为什么会这样,以及可能发生什么引起吗?到目前为止一切正常......

谢谢

【问题讨论】:

这可能不是这里的问题,但在ReadJonsPkg 你应该使用按位或:std::fstream::binary | std::fstream::in 不,那是问题所在。先生,您赢得了互联网。做出回答,我会很乐意标记并支持它 ...我刚刚开始深入挖掘 :) 【参考方案1】:

ReadJonsPkg 中,您应该使用按位或:std::fstream::binary | std::fstream::in

【讨论】:

以上是关于Boost序列化在一定文件大小后抛出“输入流错误”的主要内容,如果未能解决你的问题,请参考以下文章

使用 Boost 发送序列化结构但得到输入流错误

使用 boost 套接字的 Boost 序列化失败

C# ZipFile在成功操作后抛出异常[重复]

当外部服务尚未回复时,Websphere 7.0 恰好在 180 秒后抛出 503

画布在 LoadFromJson 后抛出污染错误

GoogleApiClient 在调用 onConnected 函数后抛出“GoogleApiClient 尚未连接”