type
aList=packed record
  a:string[255];
  b:string[255];
  c:boolean;
  d:array of [1..100] string[255];
  d:array of [1..100] boolean;
end;
用Tlist管理这个记录类型的东西
怎么将aList里的东西存盘
并且正确的读出来返回到aList里

解决方案 »

  1.   

    這是我自己做的一個比較簡單的保存成品信息的單元,你可以參考一下.
    unit GoodUnit;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons, ExtCtrls, ComCtrls, DB, ADODB;type
      TGoodItem = Class(TCollectionItem)
        private
          fPartNO : String;
          fPartName : String;
          fModel : String;
          fCustomer : String;
          fProcessStatus : String;
        public
          property PartNO : String read fPartNO write fPartNO;
          property PartName : String read fPartName write fPartName;
          property Model : String read fModel write fModel;
          property Customer : String read fCustomer write fCustomer;
          property ProcessStatus : String read fProcessStatus write fProcessStatus;
        end;type
      TGoodItemClass = Class of TGoodItem;type
      TGoodList = Class(TCollection)
        private
          fGoodItem : TGoodItem;
          function GetGoodItem(Index : integer) : TGoodItem;
          procedure SetGoodItem(Index : integer; Value : TGoodItem);
        public
          Constructor Create(GoodItemClass : TGoodItemClass);
          function Add : TGoodItem;
          procedure LoadFromFile(FileName : String);
          procedure LoadFromStream(S : TStream);
          procedure SaveToFile(FileName : String);
          procedure SaveToStream(S : TStream);
          property Items[Index: Integer]: TGoodItem read GetGoodItem write SetGoodItem; default;
        end;type
      TGoodListWrapper = class(TComponent)
      private
        FGoodList: TGoodList;
      published
        property GoodList: TGoodList read FGoodList write FGoodList;
      end;implementationconstructor TGoodList.Create(GoodItemClass: TGoodItemClass);
    begin
      inherited Create(GoodItemClass);
    end;function TGoodList.Add: TGoodItem;
    begin
      Result := TGoodItem(inherited Add);
    end;function TGoodList.GetGoodItem(Index: Integer): TGoodItem;
    begin
      Result := TGoodItem(inherited Items[Index]);
    end;procedure TGoodList.LoadFromFile(Filename: string);
    var
      S: TFileStream;
    begin
      S := TFileStream.Create(Filename, fmOpenRead);
      try
        LoadFromStream(S);
      finally
        S.Free;
      end;
    end;procedure TGoodList.LoadFromStream(S: TStream);
    var
      Wrapper: TGoodListWrapper;
    begin
      Wrapper := TGoodListWrapper.Create(nil);
      try
        Wrapper.GoodList := TGoodList.Create(nil);
        S.ReadComponent(Wrapper);
        Assign(Wrapper.GoodList);
      finally
        Wrapper.GoodList.Free;
        Wrapper.Free;
      end;
    end;procedure TGoodList.SaveToFile(Filename: string);
    var
      S: TStream;
    begin
      S := TFileStream.Create(Filename, fmCreate);
      try
        SaveToStream(S);
      finally
        S.Free;
      end;
    end;procedure TGoodList.SaveToStream(S: TStream);
    var
      Wrapper: TGoodListWrapper;
    begin
      Wrapper := TGoodListWrapper.Create(nil);
      try
        Wrapper.GoodList := Self;
        S.WriteComponent(Wrapper);
      finally
        Wrapper.Free;
      end;
    end;procedure TGoodList.SetGoodItem(Index: Integer; Value: TGoodItem);
    begin
      Items[Index].Assign(Value);
    end;end.
     
      

  2.   

    很簡單的,不知道 sixgj(轰炸机) 老兄對哪里不明白呀,主要就是利用了流(Stream)技術保存的.其它的添加,獲取某一項的數據都是通過繼承TCollection的本身方法.
      

  3.   

    type
    aList=packed record
      a:string[255];
      b:string[255];
      c:Boolean;
      d:array [1..100] of string[255];
      e:array [1..100] of boolean;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      f: file;
      al: aList;
    begin
      // 写入
      FillChar(al, sizeof(al), 0);
      al.a := 'abc';
      al.b := 'bbb';
      al.c := True;
      al.d[1] := 'ddd';
      al.e[1] := True;
      AssignFile(f, 'd:\test');
      Rewrite(f, SizeOf(al));   // 创建文件并设定记录大小
      BlockWrite(f, al, 1);     // 写入整个记录
      CloseFile(f);  // 读取
      AssignFile(f, 'd:\test');
      Reset(f, SizeOf(al));     // 打开文件并设定记录大小
      BlockRead(f, al, 1);      // 读取整个记录
      CloseFile(f);
    end;
      

  4.   

    开发人员指南上文件部分有个例子和你的一样叫做:类型文件的处理:
    type
    aList=packed record
      a:string[255];
      b:string[255];
      c:boolean;
      d:array of [1..100] string[255];
      d:array of [1..100] boolean;
    end;DataFile: File of aListvar 
      aList1:aList;
      DataFile: File of aList
    begin
      AssignFile(DataFile,'aList.bat');
      Reset(DataFile);
      try
        if not eof(DataFile) then
           Read(DataFile,aList1);
       finally
        CloseFile(DataFile);
      end;
    end;
      

  5.   

    type
    aList=packed record
      a:string[255];
      b:string[255];
      c:Boolean;
      d:array [1..100] of string[255];
      e:array [1..100] of boolean;
    end;var list:TList;
        aRecord:^aList;procedure SaveListToFile(FileName: string);
    var
      i: Integer;
      f: file;
    begin
      AssignFile(f, FileName);
      Rewrite(f, SizeOf(aList));
      for i := 0 to list.Count - 1 do
        BlockWrite(f, list[i]^, 1);
      CloseFile(f);
    end;procedure LoadListFromFile(FileName: string);
    var
      numRead: Integer;
      f: file;
    begin
      AssignFile(f, FileName);
      Reset(f, SizeOf(aList));
      repeat
        New(aRecord);
        BlockRead(f, aRecord^, 1, numRead);
        if numRead = 1 then list.Add(aRecord);
      until numRead < 1;
      CloseFile(f);
    end;
      

  6.   

    非常抱歉,现在才过来结贴
    这个问题现在已解决了,特别谢谢大家
    特别谢谢sysu(死树)
    分不多,大家不要介意