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. Problems with structures

Problems with structures

Scheduled Pinned Locked Moved C / C++ / MFC
databasesqlitehelpquestion
7 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.
  • R Offline
    R Offline
    rahul kulshreshtha
    wrote on last edited by
    #1

    I have a structure, suppose USER_INFO. When my program starts it serializes this structure from an archive file. My program uses these values, modifies few of them and when my program exists it serializes all values to that file again which it can read again on next start up. This way I stores the state of the application. Now the problem is that when I add a new field (or structure member variable) to structure. It can not read the old values from the file because structure size is now changed. I want a solution to overcome this problem. Currently an alternative way which I am using is introducing a new structure, read the old data in old structure, copy old data from old structure to new structure and write the new structure to the file. But it is not a good solution. Can Sqlite db give a better solution because adding a new filed in db is not a big problem?

    R J 2 Replies Last reply
    0
    • R rahul kulshreshtha

      I have a structure, suppose USER_INFO. When my program starts it serializes this structure from an archive file. My program uses these values, modifies few of them and when my program exists it serializes all values to that file again which it can read again on next start up. This way I stores the state of the application. Now the problem is that when I add a new field (or structure member variable) to structure. It can not read the old values from the file because structure size is now changed. I want a solution to overcome this problem. Currently an alternative way which I am using is introducing a new structure, read the old data in old structure, copy old data from old structure to new structure and write the new structure to the file. But it is not a good solution. Can Sqlite db give a better solution because adding a new filed in db is not a big problem?

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      Are you using MFC?

      "Real men drive manual transmission" - Rajesh.

      R 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        Are you using MFC?

        "Real men drive manual transmission" - Rajesh.

        R Offline
        R Offline
        rahul kulshreshtha
        wrote on last edited by
        #3

        yes

        R 1 Reply Last reply
        0
        • R rahul kulshreshtha

          yes

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          Then it's easy. MFC already has built-in support for having different versions of your serializable class. http://msdn.microsoft.com/en-us/library/00hh13h0%28v=vs.80%29.aspx[^] Pay attention to the IMPLEMENT_SERIAL macro, and especially to the third argument passed to it.

          "Real men drive manual transmission" - Rajesh.

          R 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            Then it's easy. MFC already has built-in support for having different versions of your serializable class. http://msdn.microsoft.com/en-us/library/00hh13h0%28v=vs.80%29.aspx[^] Pay attention to the IMPLEMENT_SERIAL macro, and especially to the third argument passed to it.

            "Real men drive manual transmission" - Rajesh.

            R Offline
            R Offline
            rahul kulshreshtha
            wrote on last edited by
            #5

            Looks like it can be helpful. I can specify schema version in that and at them time of reading I can check for the schema version. However I was storing whole cmap into file. I am not using << or >> operators to read and write each element one by one. So using schema version I can detect version but still I need to create another structure to load new schema version data while old schema data can be loaded into old structure. I want a solution in which I can read old schema data into new structures. So that I don't have to keep both structure versions in code. It should eliminate copying from one structure(old structure) to another structure(new structure).

            R 1 Reply Last reply
            0
            • R rahul kulshreshtha

              Looks like it can be helpful. I can specify schema version in that and at them time of reading I can check for the schema version. However I was storing whole cmap into file. I am not using << or >> operators to read and write each element one by one. So using schema version I can detect version but still I need to create another structure to load new schema version data while old schema data can be loaded into old structure. I want a solution in which I can read old schema data into new structures. So that I don't have to keep both structure versions in code. It should eliminate copying from one structure(old structure) to another structure(new structure).

              R Offline
              R Offline
              rahul kulshreshtha
              wrote on last edited by
              #6

              My cmap is like

              cmap

              writing to file like this

              CFile file;
              file.Open(pApp->AppFolderPath + "\\my_archive_file", CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
              CArchive ar(&file, CArchive::store);

              	pApp->cmap\_st\_user\_info.Serialize(ar);	
              	ar.Close();
              	file.Close();
              
              1 Reply Last reply
              0
              • R rahul kulshreshtha

                I have a structure, suppose USER_INFO. When my program starts it serializes this structure from an archive file. My program uses these values, modifies few of them and when my program exists it serializes all values to that file again which it can read again on next start up. This way I stores the state of the application. Now the problem is that when I add a new field (or structure member variable) to structure. It can not read the old values from the file because structure size is now changed. I want a solution to overcome this problem. Currently an alternative way which I am using is introducing a new structure, read the old data in old structure, copy old data from old structure to new structure and write the new structure to the file. But it is not a good solution. Can Sqlite db give a better solution because adding a new filed in db is not a big problem?

                J Offline
                J Offline
                John R Shaw
                wrote on last edited by
                #7

                The only problem I see is the non-portability of writing the structure to the file instead of individual items. That is compiler structure [type] alignment dependent and may change from one compiler to the next or one version of the compiler to the next. Your solution sounds like a simple application of the KISS principle and avoids the problems of more complex solutions. Just add a header of some kind to the file. The header should contain an identifier, marking the file as the correct type, and a version number indicating the format type (a.k.a. data type [structure type]). This also has the advantage of being cross-language and cross-platform supportable. It sounds like all the data is read into memory at start up. In that case you only need one method (function) to read the file and convert it to the internal data structure at start up and one for writing it (if it has changed) out when shutting down. I used the above method in a commercial application (originally written in C) that supports file formats going back over 20 years. This is comparable to reading and writing Word or other document types that contain version specific format support.

                INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone

                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