我在程序中定义了一个动态长度的记录结构,该结构包含一个动态长度的数组记录: 
type
  TCite = record
    id: string[5];
    name: string[8];
    depict: string[30];
  end;
type
  TAA = record
    a: string[5];
    b: integer;
    cite: array of TCite;
  end;
var
  aa: array of TAA;
如何将数组的内容保存到一个文件,并正确的读出来?我以前用的是固定长度的如下定义:
 type
  TAA = record
    a: string[5];
    b: integer;
  cite:string[20];
 end;
保存:
procedure TForm1.Button3Click(Sender: TObject);
var
  I: Integer;
  xFile: TMemoryStream;
  path: string;
begin
  i := length(aa);
  if i > 0 then
  begin
    path := ExtractFilePath(Application.EXEName) + '\aa.dat';
    xFile := TMemoryStream.Create;
    xFile.Write(i, sizeof(i));
    for i := 0 to high(aa) do
      xFile.Write(aa[i], sizeof(aa[i]));
    xFile.SaveToFile(path);
    xFile.Free;
  end;
end;
打开:
procedure TForm1.Button4Click(Sender: TObject);
var
  c_path: string;
  xFile: TMemoryStream;
  i, j: integer;
begin
  c_path := ExtractFilePath(Application.EXEName) + '\aa.dat';  xFile := TMemoryStream.Create;
  xFile.LoadFromFile(c_path);
  xFile.Read(j, sizeof(j));
  for i := 0 to j - 1 do
  begin
    setlength(aa, i + 1);
    xFile.Read(aa[i], sizeof(aa[i]));
  end;
  xFile.Free;
end;
固定长度的保存和打开都可以实现,但不知怎么保存包含动态长度的数组。

解决方案 »

  1.   

    往文件中写的时候,第一行写count:143,记录有多少个,然后读取的时候,先读取这个长度,然后再根据这个长度读取数据
      

  2.   

    我在结构体中加入count来记录数组cite的个数,重新定义如下:
    type
      TAA = record
        a: string[5];
        b: integer;
        count:integer;
        cite:array of TCite;
      end;
    保存:
    procedure TForm1.Button3Click(Sender: TObject);
    var
      I,j: Integer;
      xFile: TMemoryStream;
      path: string;
    begin
      i := length(aa);
      if i > 0 then
      begin
        path := ExtractFilePath(Application.EXEName) + '\aa.dat';
        xFile := TMemoryStream.Create;
        xFile.Write(i, sizeof(i));
        for i := 0 to high(aa) do
        begin
          xFile.Write(aa[i], sizeof(aa[i]));
          for j := 0 to aa[i].count - 1 do    // Iterate
          begin
             xFile.Write(aa[i].cite[j], sizeof(aa[i].cite[j]));
          end;    // for
        end;
        xFile.SaveToFile(path);
        xFile.Free;
      end;
    end;
    打开:
    procedure TForm1.Button4Click(Sender: TObject);
    var
      c_path: string;
      xFile: TMemoryStream;
      i, j,p: integer;
    begin
      c_path := ExtractFilePath(Application.EXEName) + '\aa.dat';  xFile := TMemoryStream.Create;
      xFile.LoadFromFile(c_path);
      xFile.Read(j, sizeof(j));
      for i := 0 to j - 1 do
      begin
        setlength(aa, i + 1);
        xFile.Read(aa[i], sizeof(aa[i]));
        for p := 0 to aa[i].count - 1 do    // Iterate
        begin
          setlength(aa[i].cite, p + 1);
          xFile.Read(aa[i].cite[p], sizeof(aa[i].cite[p]));
        end;    // for
      end;
      xFile.Free;
    end;
    执行打开操作时会出错,是不是我哪里写错了?
      

  3.   

    wudi_1982:单步执行时在打开时
    setlength(aa[i].cite, p + 1);
    出错。
      

  4.   

    Delphi不是可以定义类型文件吗(file of XXXX)?直接写,直接读嘛。
    BCB中可以ReadFile,WriteFile也很方便。
    流操作也可以啊。