MFC Serialization
-
Hello All, I am pretty new to MFC and Visual C++. I would want a little help on MFC serialization. If possible could some one please give a small piece of code as to serialize an object to a file and then deserialize it? thanks in advance.
Keshav Kamat :) India
-
Hello All, I am pretty new to MFC and Visual C++. I would want a little help on MFC serialization. If possible could some one please give a small piece of code as to serialize an object to a file and then deserialize it? thanks in advance.
Keshav Kamat :) India
-
Hello All, I am pretty new to MFC and Visual C++. I would want a little help on MFC serialization. If possible could some one please give a small piece of code as to serialize an object to a file and then deserialize it? thanks in advance.
Keshav Kamat :) India
If you are using the Doc/View architecture, you can do some simple document writing:
void CYourDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring()) {
// here we are writing
ar << an_int
<< a_double
<< a_CString
<< a_short;
} else {
// here we are reading
ar >> an_int
>> a_double
>> a_CString
>> a_short;
}Make sure the order and size of the data you write is identical to the order and size of the data you read, otherwise kaboom. If you want to serialize your own classes, you need to inherit them from CObject. It is also a bit more complicated. Websearch for DECLARE_SERIAL, IMPLEMENT_SERIAL. These are macros that you will need to add to your class. Finally, when you actually write the code to do the serialization of your class objects, you will need to over-ride Serialize(...), which your class will inherit from CObject. In your Serialize(...) over-ride, you will implement the necessary code to write your class object to a file. It will be pretty much the same as the sample code above. So, e.g., your class has an int size; member variable, you will do ar << size; in your Serialize(...) over-ride. Max Max
-
thanks. this was the exact thing i was looking for. I wanted the concept of the CFILE.
Keshav Kamat :) India
-
If you are using the Doc/View architecture, you can do some simple document writing:
void CYourDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring()) {
// here we are writing
ar << an_int
<< a_double
<< a_CString
<< a_short;
} else {
// here we are reading
ar >> an_int
>> a_double
>> a_CString
>> a_short;
}Make sure the order and size of the data you write is identical to the order and size of the data you read, otherwise kaboom. If you want to serialize your own classes, you need to inherit them from CObject. It is also a bit more complicated. Websearch for DECLARE_SERIAL, IMPLEMENT_SERIAL. These are macros that you will need to add to your class. Finally, when you actually write the code to do the serialization of your class objects, you will need to over-ride Serialize(...), which your class will inherit from CObject. In your Serialize(...) over-ride, you will implement the necessary code to write your class object to a file. It will be pretty much the same as the sample code above. So, e.g., your class has an int size; member variable, you will do ar << size; in your Serialize(...) over-ride. Max Max
Thanks. really appreciate it. I know how to work with all the components individually. the problem is putting all together in vc++ 6.0 as an application. thanks anyways. I got a pretty decent idea now.
Keshav Kamat :) India