Hi all,
Assume I have a class
class A
{
char *attr1,*attr2;
public:
. . .
};
How to save the object of this class to file in a binary format and read it back?
Thanks.
Hi all,
Assume I have a class
class A
{
char *attr1,*attr2;
public:
. . .
};
How to save the object of this class to file in a binary format and read it back?
Thanks.
Read about serialization. For example the Boost serialization library. They have a nice definition:
Here, we use the term "serialization" to mean the reversible deconstruction of an arbitrary set of C++ data structures to a sequence of bytes. Such a system can be used to reconstitute an equivalent structure in another program context. Depending on the context, this might used implement object persistence, remote parameter passing or other facility. In this system we use the term "archive" to refer to a specific rendering of this stream of bytes. This could be a file of binary data, text data, XML, or some other created by the user of this library.
That said, such a solution is the 5-kg hammer that solves most serialization problems. It may be the case that you need something far simpler. For example, if you object only consists of a few char*
attributes you can save it in a simpler way. A general solution, however, would allow you flexibility in case your object gets more complex in the future.
Instead of Binary, You might want to try xml.
The (field=value) format is very easily parsed back too.
Also checkout this Qustion:
http://stackoverflow.com/questions/974815/how-to-save-c-object-into-a-xml-file-and-restore-back
GoodLUCK!!
For efficient, fast and extensible binary serialization format, take a look at Google protocol buffers.