unit RecFile;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;type Student=Record
       Name:String[15];
       Age :integer;
end;
StudentFile=File of Student;type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Edit1: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Edit2: TEdit;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  MyFile:StudentFile;
  Varstd:Student;
  fn:string;
  rc:word;
implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin
  AssignFile(MyFile,'student.rec');
  Reset(MyFile);
  caption:= '记录大小为:'+IntToStr(Sizeof(Varstd))
            +'  共有 '+IntToStr(FileSize(MyFile))
            +' 条记录';
  CloseFile(MyFile);
  rc:=0;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  VarStd.Name :=Edit1.Text ;
  VarStd.Age  :=StrToInt(Edit2.Text);
  AssignFile(MyFile,'student.rec');
  Reset(MyFile);
  Seek(MyFile,FileSize(MyFile));
  Write(MyFile,Varstd);
  CloseFile(MyFile);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
  if fn='' then
    if OpenDialog1.Execute then
      fn:=OpenDialog1.FileName
    else
      exit;
  AssignFile(MyFile,fn);
  Reset(MyFile);
  if rc<FileSize(MyFile) then
  begin
    Seek(MyFile,rc);
    Read(MyFile,VarStd);
    if TButton(Sender).Name=Button2.Name then
    begin
      if rc<>FileSize(MyFile)-1 then
        Inc(rc);
    end
    else
    begin
      if rc>0 then
        Dec(rc);
    end;
  end;
  Label4.Caption := Varstd.Name;
  Label3.Caption := IntToStr(Varstd.Age);
  CloseFile(MyFile);
end;end.