Delphi 记录文件实例与调用

 

unit MyRec;

interface
  
uses
  windows,messages,classes,forms,sysutils;
type
  TPersonRec
=packed record
    id:Integer;
    state:
string[1];
    path:
string[50];
    pic:
string[35];
    TimeTick : TDateTime;
  
end;
  TMyrec
=class(Tfilestream)
  
private
    
function GetCurRec:longint;
  
protected
  
public
    
function GetRecSize:longint;virtual;
    
procedure SetCurRec(RecNum:longint);
    
procedure DeleteRec(RecNum:integer);
    
function GetNumRecs:longint;
    
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(RecNum:integer);
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;
  Reccount:integer;
begin
  Myrec :
= TMyrec.Create(Apppath+'History.dat',fmOpenRead or fmShareDenyWrite);
  Myrec.first;
  Reccount:
=Myrec.GetNumRecs;
  
for i:=1 to Reccount do
  
begin
    Myrec.ReadRec(Rec);
    Rec.xxxx;
    
//Myrec.AppandRec(Rec); 
    Myrec.nextRec;
 
end;  
end;

 

posted @ 2009-04-13 13:39  幽灵湖  阅读(666)  评论(0)    收藏  举报