Requirement
After user updated software version to newest, when loading an old version file or db, the newest software should read the old file correctly. This needs the software have the ability of backward compatibility of files.
Basic Principle
1st, save an file version No into file, maybe we can call it as FileVersionNo. Notice that the version No is not software version No.
2st, when loading file, read the FileVersionNo from the file, set it as the variable LoadedFileVersionNo.
3rd, we call the file version no that the software support currently as CurrentFileVersionNo. In the process of loading, compare the LoadedFileVersionNo with CurrentFileVersionNo, according to the result, the code should run different flow.
if (LoadedFileVersionNo < CurrentFileVersionNo ) {
/// read the data according to old file version data format.
} else {
/// read the data according to the current file version data format.
}
Warm Tips
- The file header should contain a token/code for file format verification.
- The file version No should be written in file header, not in file body or file tail, because it is a variable to control the loading process and we should read it an begining.