当读写单个记录时一切正常,但是如果有多个记录时可写,但读不正常,不是出来乱码就是无法执行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;
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;
wwfile.Read(fcost,sizeof(fcost)) ;<<-------sizeof(fcost)? do you know the fcost size? you don't know ,and I do not know other!
string[len]//定长
array[0..80] of char//字符数组
另外一种方法就是先写入string的长度然后再写入内容