那位有关记录文件为二进制,记录内容为record类型的例子?(不惜血本)(0分)

  • 主题发起人 主题发起人 wanglei15
  • 开始时间 开始时间
W

wanglei15

Unregistered / Unconfirmed
GUEST, unregistred user!
那位有关记录文件为二进制,记录内容为record类型的例子?(不惜血本)
 
Type
TSaveHeader = Record
scene : Integer;
hotspots : LongInt;
talk : LongInt;
hype : LongInt;
End;

Var
SaveHeader : TSaveHeader;

Procedure OpenSaveFile(fname : String);
Var
f : File;
i : Integer;
Begin
AssignFile(f, fname);
Reset(f, 1);
BlockRead(f, SaveHeader, Sizeof(TSaveHeader));
{ get one set of records }
Seek(f, SaveHeader.hotspots);
For i := 1 To 50 Do
BlockRead(f, somevar, sizeof_hotspotrec);
{ and so on }
CloseFile(f);
End;

{ assuming the file is open }
Procedure GetHotspotRec(index : LongInt; Var hotspotrec : THotspot);
Var
offset : LongInt;
Begin
offset := SaveHeader.hotspots + index * Sizeof(THotSpot);
Seek(f, offset);
BlockRead(f, hotspotrec, Sizeof(THotspot));
End;
 
完整的一个:
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.
 
后退
顶部