您好,登錄后才能下訂單哦!
1,關于序列化.png 2,關于序列化.png 3,關于序列化.png 將結構體寫入文件 name存在空格,C++的序列化相當的脆弱 C++ 序列化 二進制版 C風格的 序列化,總是正常的 boost 序列化C風格 寫入到文件simple boost 序列化C++風格 寫入到文件simple boost 序列化從文件中讀取 非侵入式的 boost序列化 缺點:所有的成員必須是public 可序列化的成員 派生類的序列化,問題引出,父類的成員增刪對序列化有影響.耦合度太高 派生類的序列化 問題解決 指針的序列化 兩個指針指向同一片區域,序列化到文件,只保存一份 智能指針的序列化 數組的序列化 指針數組的序列化 STL容器的序列化 將對象序列化為XML描述
將結構體寫入文件
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { string name; string email; int age; }; ostream& operator<<(ostream& os,const User& user ) { //空格分開,方便stream的讀入 return os<<user.name<<" "<<user.email<<" "<<user.age; } istream& operator>>(istream& is,User& user ) { return is>>user.name>>user.email>>user.age; } int main() { User user1 = {"chunli","li_chunli@mail.com",24}; User user2 = {"林沖","林沖@email.com",30}; ofstream fout("user.txt");//為了演示,省略檢查機制 fout<< user1 <<endl;//寫入到文件 fout<< user2 <<endl;//寫入到文件 User user3; User user4; ifstream fin("user.txt"); fin >> user3; fin >> user4; cout << user3 << endl; cout << user4 << endl; return 0; } chunli@Linux:~/boost$ > user.txt chunli@Linux:~/boost$ g++ main.cpp && ./a.out chunli li_chunli@mail.com 24 林沖 林沖@email.com 30 chunli@Linux:~/boost$ cat user.txt chunli li_chunli@mail.com 24 林沖 林沖@email.com 30 chunli@Linux:~/boost$
name存在空格,C++的序列化相當的脆弱
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { string name; string email; int age; }; ostream& operator<<(ostream& os,const User& user ) { //空格分開,方便stream的讀入 return os<<user.name<<" "<<user.email<<" "<<user.age; } istream& operator>>(istream& is,User& user ) { return is>>user.name>>user.email>>user.age; } int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林沖","林沖@email.com",30}; ofstream fout("user.txt");//為了演示,省略檢查機制 fout<< user1 <<endl;//寫入到文件 fout<< user2 <<endl;//寫入到文件 User user3; User user4; ifstream fin("user.txt"); fin >> user3; fin >> user4; cout << user3 << endl; cout << user4 << endl; return 0; } chunli@Linux:~/boost$ >user.txt chunli@Linux:~/boost$ g++ main.cpp && ./a.out li chunli 0 335933440 chunli@Linux:~/boost$ cat user.txt li chunli li_chunli@mail.com 24 林沖 林沖@email.com 30 chunli@Linux:~/boost$
C++ 序列化 二進制版
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { string name; string email; int age; }; int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林沖","林沖@email.com",30}; ofstream fout("user.bin");//為了演示,省略檢查機制 fout.write((const char*)&user1,sizeof(struct User)); fout.write((const char*)&user2,sizeof(struct User)); fout.close(); User user3; User user4; ifstream fin("user.bin"); fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.close(); return 0; } chunli@Linux:~/boost$ g++ main.cpp -Wall -g && ./a.out li chunli,li_chunli@mail.com,24 林沖,林沖@email.com,30 *** Error in `./a.out': double free or corruption (fasttop): 0x00000000008a90b0 *** 已放棄 (核心已轉儲) chunli@Linux:~/boost$
C風格的 序列化,總是正常的
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { char name[100]; char email[100]; int age; }; int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林沖","林沖@email.com",30}; ofstream fout("user.bin");//為了演示,省略檢查機制 fout.write((const char*)&user1,sizeof(struct User)); fout.write((const char*)&user2,sizeof(struct User)); fout.close(); User user3; ifstream fin("user.bin"); fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.close(); return 0; } chunli@Linux:~/boost$ g++ main.cpp -Wall -g && ./a.out li chunli,li_chunli@mail.com,24 林沖,林沖@email.com,30 chunli@Linux:~/boost$
boost 序列化C風格 寫入到文件simple
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { char name[16]; char email[64]; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; ofstream fout("user_info");//為了演示,省略檢查機制 boost::archive::text_oarchive oa(fout); oa << user1; oa << user2; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 16 -26 -103 -70 -27 -92 -102 -26 -104 -97 0 0 0 0 0 0 0 64 -27 -112 -76 -25 -108 -88 64 101 109 97 105 108 46 99 111 109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40 16 -24 -79 -71 -27 -83 -112 -27 -92 -76 0 0 0 0 0 0 0 64 -26 -98 -105 -27 -122 -78 64 101 109 97 105 108 46 99 111 109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 chunli@Linux:~/boost$
boost 序列化C++風格 寫入到文件simple
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; ofstream fout("user_info");//為了演示,省略檢查機制 boost::archive::text_oarchive oa(fout); oa << user1; oa << user2; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 9 智多星 16 吳用@email.com 40 9 豹子頭 16 林沖@email.com 30 chunli@Linux:~/boost$
boost 序列化從文件中讀取
chunli@Linux:~/boost$ >user_info chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; ofstream fout("user_info");//為了演示,省略檢查機制 boost::archive::text_oarchive oa(fout); oa << user1 << user2; fout.close(); User user_tmp; ifstream fin("user_info"); boost::archive::text_iarchive ia(fin); ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星,吳用@email.com,40 豹子頭,林沖@email.com,30 chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 9 智多星 16 吳用@email.com 40 9 豹子頭 16 林沖@email.com 30chunli@Linux:~/boost$
非侵入式的 boost序列化 缺點:所有的成員必須是public
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; }; template<class Archive> void serialize(Archive &ar,User &user ,const unsigned version) { ar & user.name & user.email & user.age; } int main() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; ofstream fout("user_info");//為了演示,省略檢查機制 boost::archive::text_oarchive oa(fout); oa << user1 << user2; fout.close(); User user_tmp; ifstream fin("user_info"); boost::archive::text_iarchive ia(fin); ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星,吳用@email.com,40 豹子頭,林沖@email.com,30 chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 9 智多星 16 吳用@email.com 40 9 豹子頭 16 林沖@email.com 30chunli@Linux:~/boost$ chunli@Linux:~/boost$
可序列化的成員
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct Group { string gid; User leader; vector<User> members; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & gid & leader & members; } }; ostream& operator<<(ostream& os,const User& user) { return os<<user.name<<", "<<user.email<<", "<<user.age<<endl; } int main() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; User user3 = {"及時雨","宋江@email.com",42}; User user4 = {"黑旋風","李逵@email.com",37}; Group group; group.gid = "8080"; group.leader = user1; group.members.push_back(user2); group.members.push_back(user3); group.members.push_back(user4); //寫入到文件 ofstream fout("group_info"); boost::archive::text_oarchive oa(fout); oa << group; fout.close(); //從文件中讀取 Group group2; ifstream fin("group_info"); boost::archive::text_iarchive ia(fin); ia >> group2; cout << group2.leader ; //copy(group2.members.begin(),group2.members.end(),ostream_iterator<User>(cout," OK")); copy(group2.members.begin(),group2.members.end(),ostream_iterator<User> (cout)); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星, 吳用@email.com, 40 豹子頭, 林沖@email.com, 30 及時雨, 宋江@email.com, 42 黑旋風, 李逵@email.com, 37 chunli@Linux:~/boost$ cat group_info 22 serialization::archive 14 0 0 4 8080 0 0 9 智多星 16 吳用@email.com 40 0 0 3 0 9 豹子頭 16 林沖@email.com 30 9 及時雨 16 宋江@email.com 42 9 黑旋風 16 李逵@email.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
派生類的序列化,問題引出,父類的成員增刪對序列化有影響.耦合度太高
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct VIP:public User { int level; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & email;//將成員序列化到IO ar & level; ar & name; } }; int main() { VIP vip1; vip1.level = 100; vip1.name = "黑旋風"; vip1.email = "likui@mail.com"; ofstream fout("users_vip"); boost::archive::text_oarchive oa(fout); oa << vip1; fout.close(); VIP vip2; ifstream fin("users_vip"); boost::archive::text_iarchive ia(fin); ia >> vip2; cout << vip2.level << endl; cout << vip2.name << endl; cout << vip2.email << endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 100 黑旋風 likui@mail.com chunli@Linux:~/boost$ cat users_vip 22 serialization::archive 14 0 0 14 likui@mail.com 100 9 黑旋風chunli@Linux:~/boost$ chunli@Linux:~/boost$
派生類的序列化 問題解決
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct VIP:public User { int level; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & boost::serialization::base_object<User>(*this);//將父類序列化,不再管父類有多少個成員 ar & level; } }; int main() { VIP vip1; vip1.level = 100; vip1.name = "黑旋風"; vip1.age = 34; vip1.email = "likui@mail.com"; ofstream fout("users_vip"); boost::archive::text_oarchive oa(fout); oa << vip1; fout.close(); VIP vip2; ifstream fin("users_vip"); boost::archive::text_iarchive ia(fin); ia >> vip2; cout << vip2.level << endl; cout << vip2.name << endl; cout << vip2.email << endl; cout << vip2.age << endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 100 黑旋風 likui@mail.com 34 chunli@Linux:~/boost$ cat users_vip 22 serialization::archive 14 0 0 0 0 9 黑旋風 14 likui@mail.com 34 100chunli@Linux:~/boost$ chunli@Linux:~/boost$
指針的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函數體內不能對成員數據做任何改動 { return name; } string getEmail()const //函數體內不能對成員數據做任何改動 { return email; } int getAge()const //函數體內不能對成員數據做任何改動 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *user1 = new User("及時雨","songjiang@mail.com",37); ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; file.close(); delete user1; User *user2; ifstream fin("user.info"); //boost::archive::text_iarchive ia(fin); boost::archive::text_iarchive ia(fin); ia >> user2; cout <<"name="<<user2->getName()<<", email="<<user2->getEmail()<<", age="<< user2->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及時雨, email=songjiang@mail.com, age=37 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及時雨 18 songjiang@mail.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
兩個指針指向同一片區域,序列化到文件,只保存一份
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函數體內不能對成員數據做任何改動 { return name; } string getEmail()const //函數體內不能對成員數據做任何改動 { return email; } int getAge()const //函數體內不能對成員數據做任何改動 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *user1 = new User("及時雨","songjiang@mail.com",37); User *user2 = user1; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; oa << user2; file.close(); delete user1; User *user3; User *user4; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> user3; cout <<"name="<<user3->getName()<<", email="<<user3->getEmail()<<", age="<< user3->getAge()<< endl; ia >> user4; cout <<"name="<<user4->getName()<<", email="<<user4->getEmail()<<", age="<< user4->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及時雨, email=songjiang@mail.com, age=37 name=及時雨, email=songjiang@mail.com, age=37 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及時雨 18 songjiang@mail.com 37 0 0chunli@Linux:~/boost$ chunli@Linux:~/boost$ 問題:以指針序列化,以對象讀取,失敗
智能指針的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/shared_ptr.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函數體內不能對成員數據做任何改動 { return name; } string getEmail()const //函數體內不能對成員數據做任何改動 { return email; } int getAge()const //函數體內不能對成員數據做任何改動 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *user1 = new User("及時雨","songjiang@mail.com",37); User *user2 = user1; boost::shared_ptr<User> sp1(new User("花和尚", "魯智深@mail.com", 38)); boost::shared_ptr<User> sp2 = sp1; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; oa << user2; oa << sp1; oa << sp2; file.close(); delete user1; User *user3; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> user3; cout <<"name="<<user3->getName()<<", email="<<user3->getEmail()<<", age="<< user3->getAge()<< endl; ia >> user3; cout <<"name="<<user3->getName()<<", email="<<user3->getEmail()<<", age="<< user3->getAge()<< endl; boost::shared_ptr<User> sp3; ia >> sp3; cout <<"name="<<sp3->getName()<<", email="<<sp3->getEmail()<<", age="<< sp3->getAge()<< endl; ia >> sp3; cout <<"name="<<sp3->getName()<<", email="<<sp3->getEmail()<<", age="<< sp3->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及時雨, email=songjiang@mail.com, age=37 name=及時雨, email=songjiang@mail.com, age=37 name=花和尚, email=魯智深@mail.com, age=38 name=花和尚, email=魯智深@mail.com, age=38 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及時雨 18 songjiang@mail.com 37 0 0 0 1 0 1 9 花和尚 18 魯智深@mail.com 38 0 1chunli@Linux:~/boost$ chunli@Linux:~/boost$
數組的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/shared_ptr.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函數體內不能對成員數據做任何改動 { return name; } string getEmail()const //函數體內不能對成員數據做任何改動 { return email; } int getAge()const //函數體內不能對成員數據做任何改動 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User users[3] = { User("及時雨","songjiang@mail.com",37), User("武行者","wusong@mail.com",37), User("花和尚","luzhishen@mail.com",40) }; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << users; file.close(); User buf[3]; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> buf; cout <<buf[0].getName()<<","<<buf[0].getEmail()<<","<< buf[0].getAge()<< endl; cout <<buf[1].getName()<<","<<buf[1].getEmail()<<","<< buf[1].getAge()<< endl; cout <<buf[2].getName()<<","<<buf[2].getEmail()<<","<< buf[2].getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out 及時雨,songjiang@mail.com,37 武行者,wusong@mail.com,37 花和尚,luzhishen@mail.com,40 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 3 0 0 9 及時雨 18 songjiang@mail.com 37 9 武行者 15 wusong@mail.com 37 9 花和尚 18 luzhishen@mail.com 40chunli@Linux:~/boost$ chunli@Linux:~/boost$
指針數組的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/shared_ptr.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函數體內不能對成員數據做任何改動 { return name; } string getEmail()const //函數體內不能對成員數據做任何改動 { return email; } int getAge()const //函數體內不能對成員數據做任何改動 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *users[3] = { new User("及時雨","songjiang@mail.com",37), new User("武行者","wusong@mail.com",37), new User("花和尚","luzhishen@mail.com",40) }; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << users; file.close(); User *buf[3]; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> buf; cout <<buf[0]->getName()<<","<<buf[0]->getEmail()<<","<< buf[0]->getAge()<< endl; cout <<buf[1]->getName()<<","<<buf[1]->getEmail()<<","<< buf[1]->getAge()<< endl; cout <<buf[2]->getName()<<","<<buf[2]->getEmail()<<","<< buf[2]->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out 及時雨,songjiang@mail.com,37 武行者,wusong@mail.com,37 花和尚,luzhishen@mail.com,40 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 3 0 1 0 0 9 及時雨 18 songjiang@mail.com 37 0 1 9 武行者 15 wusong@mail.com 37 0 2 9 花和尚 18 luzhishen@mail.com 40chunli@Linux:~/boost$ chunli@Linux:~/boost$
STL容器的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct Group { string gid; User leader; vector<User> members; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & gid & leader & members; } }; ostream& operator<<(ostream& os,const User& user) { return os<<user.name<<", "<<user.email<<", "<<user.age<<endl; } int main() { User user1 = {"智多星","吳用@email.com",40}; User user2 = {"豹子頭","林沖@email.com",30}; User user3 = {"及時雨","宋江@email.com",42}; User user4 = {"黑旋風","李逵@email.com",37}; Group group; group.gid = "8080"; group.leader = user1; group.members.push_back(user2); group.members.push_back(user3); group.members.push_back(user4); //寫入到文件 ofstream fout("group_info"); boost::archive::text_oarchive oa(fout); oa << group; fout.close(); //從文件中讀取 Group group2; ifstream fin("group_info"); boost::archive::text_iarchive ia(fin); ia >> group2; cout << group2.leader ; //copy(group2.members.begin(),group2.members.end(),ostream_iterator<User>(cout," OK")); copy(group2.members.begin(),group2.members.end(),ostream_iterator<User> (cout)); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星, 吳用@email.com, 40 豹子頭, 林沖@email.com, 30 及時雨, 宋江@email.com, 42 黑旋風, 李逵@email.com, 37 chunli@Linux:~/boost$ cat group_info 22 serialization::archive 14 0 0 4 8080 0 0 9 智多星 16 吳用@email.com 40 0 0 3 0 9 豹子頭 16 林沖@email.com 30 9 及時雨 16 宋江@email.com 42 9 黑旋風 16 李逵@email.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
將對象序列化為XML描述
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/serialization/vector.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned version) { ar & BOOST_SERIALIZATION_NVP(name); ar & BOOST_SERIALIZATION_NVP(email); ar & BOOST_SERIALIZATION_NVP(age); } }; ostream& operator<<(ostream& os, const User& u) { return os << u.name << ' ' << u.email << ' ' << u.age; } struct Group { string gid; User leader; vector<User> members; friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned version) { ar & BOOST_SERIALIZATION_NVP(gid); ar & BOOST_SERIALIZATION_NVP(leader); ar & BOOST_SERIALIZATION_NVP(members); } }; int main() { Group group; group.gid = "G01"; User leader = { "tom", "tom@abc.com", 28 }; group.leader = leader; User u1 = { "tom1", "tom1@abc.com", 28 }; User u2 = { "tom2", "tom2@abc.com", 28 }; User u3 = { "tom3", "tom3@abc.com", 28 }; group.members.push_back(u1); group.members.push_back(u2); group.members.push_back(u3); ofstream fout("users_xml"); boost::archive::xml_oarchive oa(fout); oa << BOOST_SERIALIZATION_NVP(group); fout.close(); Group g2; ifstream fin("users_xml"); boost::archive::xml_iarchive ia(fin); ia >> BOOST_SERIALIZATION_NVP(g2); cout << g2.leader << endl; copy(g2.members.begin(), g2.members.end(), ostream_iterator<User> (cout,"\n")); } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out tom tom@abc.com 28 tom1 tom1@abc.com 28 tom2 tom2@abc.com 28 tom3 tom3@abc.com 28 chunli@Linux:~/boost$ cat users_xml <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization> <boost_serialization signature="serialization::archive" version="14"> <group class_id="0" tracking_level="0" version="0"> <gid>G01</gid> <leader class_id="1" tracking_level="0" version="0"> <name>tom</name> <email>tom@abc.com</email> <age>28</age> </leader> <members class_id="2" tracking_level="0" version="0"> <count>3</count> <item_version>0</item_version> <item> <name>tom1</name> <email>tom1@abc.com</email> <age>28</age> </item> <item> <name>tom2</name> <email>tom2@abc.com</email> <age>28</age> </item> <item> <name>tom3</name> <email>tom3@abc.com</email> <age>28</age> </item> </members> </group> chunli@Linux:~/boost$ chunli@Linux:~/boost$
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。