开发者

C++: Saving the class object in archived format which is having char * attributes and read it back from file

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?

Th开发者_C百科anks.


For efficient, fast and extensible binary serialization format, take a look at Google protocol buffers.


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.


You might want to try xml


Instead of Binary, You might want to try xml.

The (field=value) format is very easily parsed back too.

http://cppxmlobj.sourceforge.net/

Also checkout this Qustion:

How to save c++ object into a xml file and restore back?

GoodLUCK!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜