记录文件实例与调用

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.

调用

 var
  Myrec:TMyrec;
  Rec:TPersonRec;

Myrec := TMyrec.Create('rec.dat',fmCreate or fmShareDenyWrite);
    Rec.id :='';
    Rec.path :='';
    Rec.pic :='';
    Rec.tdate :='';
    Myrec.AppandRec(Rec);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值