In my MFC application, I am using CFile class to write data to file. I store a sequence of objects of class CParagraph using Write() method for each data member in order. I use then Read() method to read from file into memory. One of the CParagraph's members used to be of type int, but now I have to change it to size_t, as开发者_高级运维 int cannot hold data large enough. If my application reads a file created before this change, and then a saves a CParagraph object back into the file, size of size_t will be passed to the Write method instead of size of int, so the file will grow. My question is this: can the data written after the object being modified and saved be overwritten and thus corrupted because the object became larger ?
Thanks.
Yes. If anything in the file changes size, everything after it must be re-saved.
It's common to save a "version" char as the first part of a file. Then, when you need to resize a variable (or change tons of things), you can change the version when saving. Then, while loading, you can check the version, and use the coresponding code to load it. Then you can still open files from older versions. Note that this version should only change when the file format changes, not when you rebuild/release.
精彩评论