c++ how to serialize/deserialize an object? -


possible duplicate:
how save c++ object xml file , restore back?

hi,

can tell me best way seralialize , deserialize (of course) object? have class a lot of string, int values. west way create xml , after (when have xml) open , obtain values?
can give me please code examples?

to serialize/deserialize object, have this:

ofstream f( "output.bin", ios::binary );  f.write( (char *) &myobject, sizeof( myobject ) ); f.close();  // later...  ifstream f( "output.bin", ios::binary ); myclass myobject; f.read( (char *) &myobject, sizeof( myobject ) ); f.close(); 

however, has lot of drawbacks. example, application won't work object serialized in computer of different architectures. big problem objects cannot have relationship other objects, i.e., must simple objects.

that's why people use format more understandable among different architectures. text structure, xml. can use number of xml libraries, such xerces.

there various strategies, simplistic strategy write own toxml() method in each class want persistent, along corresponding [static] fromxml(), , use own xml parser.

of course, quite better use library xerces , conform needs.

as can see, these insights beyond possiblities of answer here.

you'll have read xml, xerces (or other xml parser), , figure out how apply application. can see, broad topic.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -