Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following is simple boost serialization code, but ClassA.name member can't be serialized and deserialized, anyone can help me?

C++
class ClassA
{
public:
	int number;
	char name[64];
};

template<class archive="">
void serialize(Archive & ar, ClassA& g, const unsigned int version)
{
	ar & g.number;
	ar & g.name;
};

int _tmain(int argc, _TCHAR* argv[])
{
	ClassA obj1;
	obj1.number = 21;
	sprintf_s(obj1.name, 64, "%s", "Hello World!");

	std::ofstream ofs("D:\\efg.txt", std::ios::out);
	if (!ofs.fail())  
	{  
		boost::archive::text_oarchive oa(ofs);
		oa << obj1;
		ofs.close();
	}

	ClassA obj2;
	std::ifstream ifs("D:\\efg.txt");
	if (!ifs.fail())  
	{  
		boost::archive::text_iarchive ia(ifs);
		ia >> obj2;

		ifs.close();
	}

	return 0;
}</class>
Posted
Updated 14-Oct-14 21:56pm
v2
Comments
CubbiMew 15-Oct-14 0:07am    
There is nothing special about array serialization, it just works: see boost.serialization tutorial[^]

Your code works pretty much as-is in GCC: http://coliru.stacked-crooked.com/a/869d9f2a5a7d05c4[^]

How exactly does it not work for you? Is there a compile-time error? Can you post it?
Member 11153987 15-Oct-14 0:52am    
Thanks for your reply.
I run this code in Visual Studio 2012, there is no compile-time error. int:number can be serialized correctly, but char[64]:name can't get the correct value. It's puzzling.
Output:
obj1 = {21, Hello World!}
obj2 = {21, }

1 solution

For all i know boost serialization can't serialize a char array. You should be using std::string to hold your strings and just serialize that.

If you must use a char array, you can convert it for the purpose of serialization as described here[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900