boost序列化struct

#include 
#include 
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","[email protected]",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;
}

你可能感兴趣的:(boost,c++)