先谢谢volte的回答,但我的情况是在读的时个是不知道数组的个数的。我先是声明一个类。
//学习记录
type
TLearn=record
lname:string[40];
atype:string[20];
lntype:string[20];
smint:integer;
sm:array of string[60];
expint:integer;
expstr:array of string[60];
end;
//记录的文件流类
type
TRecFile=class(TFileStream)
private
public
Function AppendRec(Learn:TLearn):boolean; //追加一个记录
Function ReadRec(var Learn:TLearn):boolean; //提取一个记录
procedure SetLearn(ed:Tedit;Comb1,comb2: TComboBox;
Memo1,Memo2: TMemo; var Learn:TLearn); //设置记录值
end;
{******************************************************************
提取动态数组的方法为
在数组前加一个字段为该数组的个数,并用循环读入。
******************************************************************}
implementation
//追加一个记录
function TRecFile.AppendRec(Learn:TLearn):boolean;
begin
try
self.Seek(0,soFromEnd);
self.Write(Learn,sizeof(learn));
result:=True;
except
result:=False;
end;
end;
//提取一个记录
function TRecFile.ReadRec(var Learn:TLearn):boolean;
var
i:integer;
begin
try
// self.Seek(0,0);
self.Read(learn.lname,length(learn.lname));
self.Seek(sizeof(learn.lname),soFromCurrent);
self.Read(learn.atype,length(learn.atype));
self.Seek(sizeof(learn.atype),soFromCurrent) ;
self.Read(learn.lntype,20);
self.Seek(sizeof(learn.lntype),1);
self.Read(learn.smint,4);
self.Seek(sizeof(learn.smint),1);
//提取动态数组
if learn.smint>0 then
begin
setlength(learn.sm,learn.smint);
for i:=0 to learn.smint-1 do
begin
self.Read(learn.sm[i],length(learn.sm[i]));
self.Seek(sizeof(learn.sm[i]),1);
end;
end;
self.Read(learn.expint,sizeof(learn.expint));
if learn.expint>0 then
begin
setlength(learn.expstr,learn.expint);
for i:=0 to learn.expint-1 do
begin
self.Read(learn.expstr[i],length(learn.expstr[i]));
self.Seek(sizeof(learn.expstr[i]),1);
end;
end;
result:=false;
except
result:=true;
end;
end;
procedure TRecFile.SetLearn(ed:Tedit;Comb1,comb2: TComboBox;
Memo1,Memo2: TMemo;var Learn:TLearn);
var
i:integer;
begin
learn.lname:=ed.Text;
learn.atype:=comb1.Text;
learn.lntype:=comb2.Text;
learn.smint:=memo1.Lines.Count;
setlength(learn.sm,learn.smint);
if learn.smint>0 then
begin
for i:=0 to learn.smint-1 do
learn.sm[i]:=memo1.Lines.Strings[i];
end;
learn.expint:=memo2.Lines.Count;
setlength(learn.expstr,learn.expint);
if learn.expint>0 then
begin
for i:=0 to learn.expint-1 do
learn.expstr[i]:=memo2.Lines.Strings[i];
end;
end;