为什么char数组成员无法通过boost序列化? [英] why char array member can't be serialized by boost?
本文介绍了为什么char数组成员无法通过boost序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是简单的boost序列化代码,但ClassA.name成员无法序列化和反序列化,任何人都可以帮助我吗?
Following is simple boost serialization code, but ClassA.name member can't be serialized and deserialized, anyone can help me?
class ClassA
{
public:
int number;
char name[64];
};
template<class archive="">
void serialize(Archive & ar, ClassA& g, const unsigned int version)
{
ar & g.number;
ar & g.name;
};
int _tmain(int argc, _TCHAR* argv[])
{
ClassA obj1;
obj1.number = 21;
sprintf_s(obj1.name, 64, "%s", "Hello World!");
std::ofstream ofs("D:\\efg.txt", std::ios::out);
if (!ofs.fail())
{
boost::archive::text_oarchive oa(ofs);
oa << obj1;
ofs.close();
}
ClassA obj2;
std::ifstream ifs("D:\\efg.txt");
if (!ifs.fail())
{
boost::archive::text_iarchive ia(ifs);
ia >> obj2;
ifs.close();
}
return 0;
}</class>
推荐答案
据我所知,boost序列化无法序列化char数组。您应该使用std :: string来保存字符串并将其序列化。
如果必须使用char数组,可以将其转换为序列化目的如此处所述[ ^ ]
For all i know boost serialization can't serialize a char array. You should be using std::string to hold your strings and just serialize that.
If you must use a char array, you can convert it for the purpose of serialization as described here[^]
这篇关于为什么char数组成员无法通过boost序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文