加载不同版本的记录

时间:2015-08-16 04:08:37

标签: delphi

我在磁盘上存储了多个版本的记录数据:

TRec_v1 = record
  Type: UInt32;
  DT1: TDateTime;
end;

TRec_v2 = record
  Type: UInt32;
  DT1: TDateTime;
  DT2: TDateTime;
end;

TRec_v3 = record
  Type: UInt32;
  DT1: TDateTime;
  DT2: TDateTime;
  DT3: TDateTime;
end;

阅读它的禁食方法是什么?

目前我使用这种方法:

  var
    Rec: TRec_v3;
    Rec1: TRec_v1;
    Rec2: TRec_v2;

  FStream := TFileStream.Create(RecPath, fmOpenRead);
  try
    if FStream.Size = SizeOf(TRec_v1) then
      // read to Rec1, assignt to Rec
    else
    if FStream.Size = SizeOf(TRec_v2) then
      // read to Rec2, assigne to Rec
    else
    if FStream.Size = SizeOf(TRec_v3) then
      // read to Rec
  finally
    FStream.Free;
  end;

注意:每个较新版本都包含先前版本+新字段

中的所有字段

1 个答案:

答案 0 :(得分:1)

我建议您创建,阅读和撰写variant record,然后使用标记区分它们:

type recordTypeName = record
  fieldList1: type1;
   ...
  fieldListn: typen;
case tag: ordinalType of
  constantList1: (variant1);
   ...
  constantListn: (variantn);
end;