unit MyRec;
interface
uses
windows,messages,classes,forms,sysutils;
type
TPersonRec=packed record
id:string[11];
path:string[6];
pic:string[35];
tdate:string[10];
end;
TMyrec=class(Tfilestream)
private
function GetNumRecs:longint;
function GetCurRec:longint;
procedure SetCurRec(RecNum:longint);
protected
function GetRecSize:longint;virtual;
public
procedure DeleteRec(var Rec);
function seekRec(RecNum:longint):longint;
function WriteRec(const Rec):longint;
function AppandRec(const Rec):longint;
function ReadRec(var Rec):longint;
procedure first;
procedure last;
procedure nextRec;
procedure previouRec;
property NumRecs:longint read GetNumRecs;
property CurRec:longint read GetCurRec Write SetCurRec;
end;
implementation
function TMyrec.GetRecSize:longint; //取得单条记录的长度
begin
Result:=sizeof(TPersonRec);
end;
function TMyRec.GetNumRecs:longint; //取得记录总数
begin
Result:=size div getRecsize;
end;
function TMyRec.GetCurRec; //取得当前所在记录号
begin
Result:=(position div GetRecsize)+1;
end;
//前往某条记录
function TMyRec.seekRec(RecNum:integer):longint;
begin
seek(0,0);
Result:=seek((RecNum-1)*GetRecSize,1);
end;
//写入记录
function TMyRec.WriteRec(const Rec):longint;
begin
Result:=write(Rec,GetRecSize);
end;
//增加记录
function TMyRec.AppandRec(const Rec):longint;
begin
seek(0,2); //to the end of then stream
Result:=write(Rec,GetRecsize);
end;
//读取记录
function TMyRec.ReadRec(var Rec):longint;
begin
result:=read(Rec,GetRecsize);
seek(-GetRecsize,1);
end;
//删除记录
procedure TmyRec.DeleteRec(var Rec);
begin
end;
//前往某条记录
procedure TMyRec.SetCurRec(RecNum:integer);
begin
if RecNum>0 then
position:=(RecNum-1)*GetRecsize
end;
//第一条
procedure TMyRec.first;
begin
seek(0,0);
end;
//最后一条
procedure TMyRec.last;
begin
seek(0,2);
seek(-getRecsize,1);
end;
//下一条
procedure TMyRec.nextRec;
begin
if ((position+GetRecsize) div GetRecsize)<GetNumRecs then
seek(GetRecsize,1)
end;
//前一条
procedure TMyRec.previouRec;
begin
if (position-GetRecsize)>=0 then
seek(-GetRecsize,1)
end;
end.
interface
uses
windows,messages,classes,forms,sysutils;
type
TPersonRec=packed record
id:string[11];
path:string[6];
pic:string[35];
tdate:string[10];
end;
TMyrec=class(Tfilestream)
private
function GetNumRecs:longint;
function GetCurRec:longint;
procedure SetCurRec(RecNum:longint);
protected
function GetRecSize:longint;virtual;
public
procedure DeleteRec(var Rec);
function seekRec(RecNum:longint):longint;
function WriteRec(const Rec):longint;
function AppandRec(const Rec):longint;
function ReadRec(var Rec):longint;
procedure first;
procedure last;
procedure nextRec;
procedure previouRec;
property NumRecs:longint read GetNumRecs;
property CurRec:longint read GetCurRec Write SetCurRec;
end;
implementation
function TMyrec.GetRecSize:longint; //取得单条记录的长度
begin
Result:=sizeof(TPersonRec);
end;
function TMyRec.GetNumRecs:longint; //取得记录总数
begin
Result:=size div getRecsize;
end;
function TMyRec.GetCurRec; //取得当前所在记录号
begin
Result:=(position div GetRecsize)+1;
end;
//前往某条记录
function TMyRec.seekRec(RecNum:integer):longint;
begin
seek(0,0);
Result:=seek((RecNum-1)*GetRecSize,1);
end;
//写入记录
function TMyRec.WriteRec(const Rec):longint;
begin
Result:=write(Rec,GetRecSize);
end;
//增加记录
function TMyRec.AppandRec(const Rec):longint;
begin
seek(0,2); //to the end of then stream
Result:=write(Rec,GetRecsize);
end;
//读取记录
function TMyRec.ReadRec(var Rec):longint;
begin
result:=read(Rec,GetRecsize);
seek(-GetRecsize,1);
end;
//删除记录
procedure TmyRec.DeleteRec(var Rec);
begin
end;
//前往某条记录
procedure TMyRec.SetCurRec(RecNum:integer);
begin
if RecNum>0 then
position:=(RecNum-1)*GetRecsize
end;
//第一条
procedure TMyRec.first;
begin
seek(0,0);
end;
//最后一条
procedure TMyRec.last;
begin
seek(0,2);
seek(-getRecsize,1);
end;
//下一条
procedure TMyRec.nextRec;
begin
if ((position+GetRecsize) div GetRecsize)<GetNumRecs then
seek(GetRecsize,1)
end;
//前一条
procedure TMyRec.previouRec;
begin
if (position-GetRecsize)>=0 then
seek(-GetRecsize,1)
end;
end.
调用
var
Myrec:TMyrec;
Rec:TPersonRec;
Myrec := TMyrec.Create('rec.dat',fmCreate or fmShareDenyWrite);
Rec.id :='';
Rec.path :='';
Rec.pic :='';
Rec.tdate :='';
Myrec.AppandRec(Rec);