gpt4 book ai didi

c++ - Boost 序列化在特定文件大小后抛出 "input stream error"

转载 作者:搜寻专家 更新时间:2023-10-31 01:11:05 25 4
gpt4 key购买 nike

我正在使用 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

关于c++ - Boost 序列化在特定文件大小后抛出 "input stream error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15314747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com