c_cpp 序列化和反序列化对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 序列化和反序列化对象相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cassert>
 
class Student
{
public:
	std::string mName;
	std::string mId;
	std::string mPhoneNumber;
 
	Student(std::string id = "", std::string name = "", std::string phone = "") : mId(id), mName(name), mPhoneNumber(phone)
	{}
 
	bool operator==(const Student & obj)
	{
		return (mId == obj.mId) && (mName == obj.mName) && (mPhoneNumber == obj.mPhoneNumber);
	}
 
	/*
	 * Write the member variables to stream objects
	 */
	friend std::ostream & operator << (std::ostream &out, const Student & obj)
	{
		out << obj.mId << "\n" <<obj.mName<<"\n"<<obj.mPhoneNumber<<std::endl;
		return out;
	}
	/*
	 * Read data from stream object and fill it in member variables
	 */
	friend std::istream & operator >> (std::istream &in,  Student &obj)
	{
		in >> obj.mId;
		in >> obj.mName;
		in >> obj.mPhoneNumber;
		return in;
	}
 
 
};
 
int main()
{
	Student stud1("1","Jack", "4445554455");
	Student stud2("4","Riti", "4445511111");
	Student stud3("6","Aadi", "4040404011");
 
	// Open the File
	std::ofstream out("students.txt");
 
	// Write objects to file
	out<<stud1;
	out<<stud2;
	out<<stud3;
 
	out.close();
 
	// Open the File
	std::ifstream in("students.txt");
 
	Student student1;
	Student student2;
	Student student3;
 
	// Read objects from file and fill in data
	in>>student1;
	in>>student2;
	in>>student3;
 
	in.close();
 
	// Compare the Objects
	assert(stud1==student1);
	assert(stud2==student2);
	assert(stud3==student3);
 
	return 0;
}

以上是关于c_cpp 序列化和反序列化对象的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 297.序列化和反序列化二叉树 - 难度硬 - 2018.9.14

无法反序列化当前的JSON对象,为啥

(JSON) 序列化和反序列化,这个是啥意思呀?

序列化和反序列化

序列化和反序列化

将对象序列化和反序列化