当读写单个记录时一切正常,但是如果有多个记录时可写,但读不正常,不是出来乱码就是无法执行PTkCost=^TkCost;
Tkcost=packed Record
   kName:string;    //名称
   kinv1, //单价金额
   kinv2,//数量
   kinv3,//合价
   kinv4:real;  //属性
   kinvFF:string;
  end;
  -----
  ---var
  Form1: TForm1;klist:tlist;implementation{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
var n:integer;ww:string;wwfile:tfilestream;fcost:Tkcost;
begin      
      if savedialog1.Execute then
      begin
        ww:=savedialog1.FileName;
        wwfile:=tfilestream.Create(ww,fmCreate or fmOpenReadWrite);
          for n:=1 to 5 do
          begin
           fcost.kName:='奇奇怪怪'+inttostr(n);
           fcost.kinv1:=-0.11*n;
           fcost.kinv2:=22;
           fcost.kinv3:=5588;
           fcost.kinv4:=0;
           fcost.kinvFF:='ABCr';
           wwfile.Write(fcost,sizeof(fcost));
          end;    
          wwfile.Free;   
      end;
end;procedure TForm1.Button1Click(Sender: TObject);
var n:integer;ww:string;wwfile:tfilestream;fcost3,fcost2:ptkcost;fcost:Tkcost;
begin
   if opendialog1.Execute then
   begin
        klist.Clear;
        ww:=opendialog1.FileName;
        wwfile:=tfilestream.Create(ww,fmOpenRead);
         for n:=1 to 5 do begin
           wwfile.Read(fcost,sizeof(fcost)) ;
           new(fcost3);
         fcost3.kName:=fcost.kName;
         fcost3.kinv1:=fcost.kinv1;
         fcost3.kinv2:=fcost.kinv2;
         fcost3.kinv3:=fcost.kinv3;
         fcost3.kinv4:=fcost.kinv4;
         fcost3.kinvFF:=fcost.kinvFF;
         klist.Add(fcost3); 
        end;
        for n:=1 to 5 do
        begin
         fcost2:=klist.Items[n-1];
         stringgrid1.Cells[n,1]:=fcost2.kName;
         stringgrid1.Cells[n,2]:=fcost2.kinvFF; 
        end; 
        
       wwfile.Free;
   end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
  klist:=tlist.Create;
end;

解决方案 »

  1.   

    for n:=1 to 5 do begin
               wwfile.Read(fcost,sizeof(fcost)) ;
               new(fcost3);
             fcost3.kName:=fcost.kName;
             fcost3.kinv1:=fcost.kinv1;
             fcost3.kinv2:=fcost.kinv2;
             fcost3.kinv3:=fcost.kinv3;
             fcost3.kinv4:=fcost.kinv4;
             fcost3.kinvFF:=fcost.kinvFF;
             klist.Add(fcost3); 
    这个地方,你每循环一次,都new(fcost3),这里可能有问题
      

  2.   

    当然要出错啦,你保存的时候,要指定长度,因string类型他的长如果不指定就要出错哦,其他类型本身就有长度的啦。哈哈,你学了一招吧
      

  3.   

    nbforyou(补风捉影) 同意
    string没有指定长度,也就是说格式长度不统一,怎么读呀
    可以这样定义 kName:array[100] of char;