Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MFC Serialization

MFC Serialization

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonhelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Keshav V Kamat 0
    wrote on last edited by
    #1

    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

    C M 2 Replies Last reply
    0
    • K Keshav V Kamat 0

      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

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      See here[^]


      Cédric Moonen Software developer
      Charting control [v1.1]

      K 1 Reply Last reply
      0
      • K Keshav V Kamat 0

        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

        M Offline
        M Offline
        maxmaven
        wrote on last edited by
        #3

        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

        K 1 Reply Last reply
        0
        • C Cedric Moonen

          See here[^]


          Cédric Moonen Software developer
          Charting control [v1.1]

          K Offline
          K Offline
          Keshav V Kamat 0
          wrote on last edited by
          #4

          thanks. this was the exact thing i was looking for. I wanted the concept of the CFILE.

          Keshav Kamat :) India

          1 Reply Last reply
          0
          • M maxmaven

            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

            K Offline
            K Offline
            Keshav V Kamat 0
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups