Unit1.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:2k
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- TPersonRec = packed record
- Name: string[40]; //姓名
- BirthDay: TdateTime; //生日
- end;
- TForm1 = class(TForm)
- btnOpenRead: TButton;
- BtnWrite: TButton;
- Memo1: TMemo;
- procedure btnOpenReadClick(Sender: TObject);
- procedure BtnWriteClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.btnOpenReadClick(Sender: TObject);
- var
- PersonRec: TPersonRec;
- DataFile: file of TPersonRec;
- i: integer;
- begin
- //关联文件
- AssignFile(DataFile, 'PersonFile.dat');
- if not FileExists('PersonFile.dat') then
- raise Exception.Create('personFile.dat不存在');
- //打开文件
- Reset(DataFile);
- Memo1.Lines.Clear;
- try
- i := 1;
- while not Eof(DataFile) do
- begin
- //读到PersonRec记录中
- Read(DataFile, PersonRec);
- Memo1.Lines.Add('******第' + IntToStr(i) + '条记录********');
- Memo1.Lines.Add('姓名: ' + PersonRec.Name);
- Memo1.Lines.Add('生日: ' + DateToStr(PersonRec.BirthDay));
- i := i + 1;
- end;
- finally
- CloseFile(DataFile);
- end;
- end;
- procedure TForm1.BtnWriteClick(Sender: TObject);
- var
- PersonRec: TPersonRec;
- DataFile: file of TPersonRec;
- begin
- //关联文件
- AssignFile(DataFile, 'PersonFile.dat');
- //文件存在,就打开,否则创建
- if FileExists('PersonFile.dat') then
- Reset(DataFile)
- else
- Rewrite(DataFile);
- PersonRec.Name := '陈鹏';
- PersonRec.BirthDay := StrtoDate('1980-01-07');
- //移动文件指针到文件尾部
- Seek(DataFile, FileSize(DataFile));
- try
- //将PersonRec记录写入到文件中
- Write(DataFile, PersonRec);
- finally
- CloseFile(DataFile);
- end;
- end;
- end.