完整的一个:
unit personrec;
interface
uses classes,dialogs,sysutils;
type
TpersonRec=packed record
Name:string[6];
sex:string[1];
address:string[50];
tel:integer;
end;
TRecordStream=class(TFileStream)
private
function GetNumRecs:longint;
function GetCurRec:longint;
procedure SetCurRec(RecNo:longint);
protected
function GetRecSize:longint;virtual;
public
function SeekRec(RecNo:longint;Origin:word):longint;
function WriteRec(const Rec):longint;
function AppendRec(const Rec):longint;
function ReadRec(var Rec):longint;
procedure First;
procedure Last;
procedure NextRec;
procedure PreviousRec;
property NumRecs:longint read GetNumRecs;
property CurRec:longint read GetCurRec write SetCurRec;
end;
implementation
function TRecordStream.GetRecSize:longint;
begin
Result:=SizeOf(TPersonRec)
end;
function TRecordStream.GetNumRecs:longint;
begin
Result:=Size div GetRecSize;
end;
function TRecordStream.GetCurRec:longint;
begin
Result:=(Position div GetRecSize)+1;
end;
procedure TRecordStream.SetCurRec(RecNo: Longint);
begin
{ This procedure sets the position to the record in the stream
specified by RecNo. }
if RecNo > 0 then
Position := (RecNo - 1) * GetRecSize
else
Raise Exception.Create('Cannot go beyond beginning of file.');
end;
function TRecordStream.SeekRec(RecNo:longint;Origin:word):longint;
begin
Result:=Seek(RecNo*GetRecSize,origin);
end;
function TRecordStream.WriteRec(const Rec):longint;
begin
Result:=Write(Rec,GetRecSize);
end;
function TRecordStream.AppendRec(const Rec):longint;
begin
Seek(0,2);
Result:=Write(Rec,GetRecSize);
end;
function TRecordStream.ReadRec(var Rec):longint;
begin
Result:=Read(Rec,GetRecSize);
seek(-GetRecSize,1);
end;
procedure TRecordStream.First;
begin
Seek(0,0);
end;
procedure TRecordStream.Last;
begin
Seek(0,2);
Seek(-GetRecSize,1);
end;
procedure TRecordStream.NextRec;
begin
if ((Position+GetRecSize) div GetRecSize)=GetNumRecs then
//raise Exception.Create ('cannot read beyond end of file')
abort
else
Seek(GetRecSize,1);
end;
procedure TRecordStream.PreviousRec;
begin
if (Position-GetRecSize >=0) then
Seek(-GetRecSize,1)
else
//raise Exception.Create ('cannot read beyond beginning of the file.');
abort;
end;
end.