Problems with structures
-
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?
-
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?
Are you using MFC?
"Real men drive manual transmission" - Rajesh.
-
Are you using MFC?
"Real men drive manual transmission" - Rajesh.
yes
-
yes
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.
-
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.
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).
-
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).
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();
-
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?
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