正如马克使用http://www.boost.org/doc/libs/1_49_0/libs/serialization/doc/index.html指出的那样,您需要将数据打包或封装到对象中并将它们序列化。您可以在此处找到示例和详细帮助。搜索一个非常简单的案例。:)
编辑:我为你做了一个快速而肮脏的例子。享受。(为格式化道歉。)
#include <cstddef> // NULL
#include <iomanip>
#include <iostream>
#include <fstream>
#include <iterator>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
class family_names
{
friend class boost::serialization::access;
std::vector<std::string> m_names;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & m_names;
}
friend std::ostream & operator<<(std::ostream &os, const family_names& fn);
public:
family_names(const std::vector<std::string>& names)
: m_names(names)
{}
family_names() {}
};
std::ostream & operator<<(std::ostream &os, const family_names& fn)
{
std::ostream_iterator<std::string> out_it (std::cout,", ");
std::copy(fn.m_names.begin(), fn.m_names.end(), out_it);
return os;
}
void save(const family_names &s, const char * filename){
// make an archive
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << s;
}
void restore(family_names &s, const char * filename)
{
// open the archive
std::ifstream ifs(filename);
boost::archive::text_iarchive ia(ifs);
// restore the family names from the archive
ia >> s;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<std::string> vec_names;
vec_names.push_back("Jason");
vec_names.push_back("Mark");
vec_names.push_back("Ludmilla");
family_names names(vec_names);
std::string filename = "c:\\Dev\\demofile.txt";
std::cout << names << std::endl;
save(names, filename.c_str());
family_names names_back;
restore(names_back, filename.c_str());
std::cout << names_back << std::endl;
return 0;
}