【发布时间】:2015-01-20 04:45:04
【问题描述】:
我正在使用 BOOST 库解析下面的 xml 文件-
<da>
<m_day Type="int">15</m_day>
<m_month Type="int">8</m_month>
<m_year Type="int">1947</m_year>
</da>
我的 cpp 代码是:
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <iostream>
#include <fstream>
typedef struct date {
unsigned int m_day;
unsigned int m_month;
unsigned int m_year;
date( int d, int m, int y) : m_day(d), m_month(m), m_year(y)
{}
date() : m_day(1), m_month(1), m_year(2000)
{}
friend std::ostream& operator << (std::ostream& out, date& d)
{
out << "day: " << d.m_day
<< " month: " << d.m_month
<< " year: " << d.m_year;
return out;
}
template<class Archive>
void serialize(Archive& archive, const unsigned int version)
{
archive & BOOST_SERIALIZATION_NVP(m_day);
archive & BOOST_SERIALIZATION_NVP(m_month);
archive & BOOST_SERIALIZATION_NVP(m_year);
}
} date;
BOOST_CLASS_IMPLEMENTATION(date, boost::serialization::object_serializable);//object_serializable);
unsigned int flags = boost::archive::no_header;
int main()
{
std::ifstream file1("archive.xml");
boost::archive::xml_iarchive ia(file1,flags);
date dr;
ia >> BOOST_SERIALIZATION_NVP(dr);
std::ofstream file("archive2.xml");
boost::archive::xml_oarchive oa(file,flags);
// date da(15, 8, 1947);
oa & BOOST_SERIALIZATION_NVP(dr);
return 0;
}
我遇到以下错误:
在抛出 'boost::archive::archive_exception' 的实例后调用终止 what(): 输入流错误 中止(核心转储)
对于没有属性的普通 xml(如下所述),上面的代码工作正常
<da>
<m_day>15</m_day>
<m_month>8</m_month>
<m_year>1947</m_year>
</da>
但是对于之前的xml文件,代码有问题吗?你能告诉我boost是否可能吗?我已经搜索了很多以找到答案,但没有得到任何答案。 提前致谢!!!
【问题讨论】:
-
这不起作用,因为您的文档不是序列化存档。 Boost 序列化格式不是免费的