使用分隔符吧,例如:
const
cSpliter = '|';
type
PMyDataStru = ^TMyDataStru;
TMyDataStru = record
Name: String[50];
Age: Integer;
Sex: Boolean;
Birthday: String[10]; // 我不知道是否是 TDateTime
end;
...
// 下面程序没有加入容错的,请自己斟酌
procedure GetMyDataFromFile(const FileName: String; DataList: TList);
var
F: TextFile;
P: PMyDataStru;
S: ShortString;
ps: PShortString;
X, Y: Integer;
procedure GetNext;
begin
Ps:= @Ps^[X];
Dec(Y, X);
PByte(Ps)^:= Y;
X:= Pos(cSpliter, Ps^);
PByte(Ps)^:= X - 1;
end;
begin
AssignFile(F, FileName);
Reset(F);
while not eof(F) do begin
New(P);
Readln(F, S); X:= 0; Y:= Length(S);
GetNext; P^.Name:= Ps^;
GetNext; P^.Age:= StrToInt(Ps^);
GetNext; P^.Sex:= (upcase(Ps^[1]) = 'M');
GetNext; P^.Birthday:= Ps^;
DataList.Add(P);
end;
CloseFile(F);
end;
procedure SaveMyDataToFile(const FileName: String; DataList: TList);
var
F: TextFile;
P: PMyDataStru;
S: ShortString;
I: Integer;
begin
AssignFile(F, FileName);
Rewrite(F);
for I:= 0 to DataList.Count - 1 do begin
P:= PMyDataStru(DataList);
S:= P^.Name + cSpliter + IntToStr(P^.Age) + cSpliter;
if P^.Sex then S:= S + 'M' else S:= S + 'F';
S:= S + cSpliter + P^.Birthday + cSpliter;
Writeln(F, S);
end;
CloseFile(F);
end;