【发布时间】:2013-10-03 06:25:08
【问题描述】:
在过去,我看过这项工作,但我从来没有真正理解应该如何完成。
假设我们有一个已知数据类型的文件,但未知长度,比如TSomething的动态数组,其中
type
TSomething = class
Name: String;
Var1: Integer;
Var2: boolean;
end;
但问题在于,这种对象类型将来可能会扩展,添加更多变量(例如Var3: String)。
然后,使用旧版本保存的文件将不包含最新的变量。
文件读取程序应该以某种方式识别 blocks 中的数据,其算法如下:
procedure Read(Path: String)
begin
// Read Array Size
// Read TSomething --> where does this record end? May not contain Var3!
// --> how to know that the next data block I read is not a new object?
end;
我已经看到与BlockRead 和BlockWrite 一起工作,并且我假设每个对象可能应该在将自身写入文件之前写入其大小,但我希望有一个示例(不一定是代码),要知道我我正在朝着正确的方向思考。
我找到的相关读物:
SO - Delphi 2010: How to save a whole record to a file?
Delphi Basics - BlockRead
SO - Reading/writing dynamic arrays of objects to a file - Delphi
SO - How Can I Save a Dynamic Array to a FileStream in Delphi?
【问题讨论】:
标签: delphi