给你发一个DEMO看看就明白了:
{ 用来操作类型文件
Used Following Function or Procedure:
procedure AssignFile(Var F,String);
procedure Reset(var F[:File;RecSize:Word]);
procedure Rewrite(var F:File[;RecSize:Word]);
procedure Seek(var F;N:Longint);
procedure Read(F,V1[,V2,...,Vn]);
procedure Write(F,V1[,V2,...,Vn]);
function Eof(var F):Boolean
procedure CloseFile(var F);
}
unit Addr;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
ButtonPrevious: TButton;
ButtonSave: TButton;
ButtonNext: TButton;
ButtonNew: TButton;
BitBtn1: TBitBtn;
RecordNumber: TPanel;
Label8: TLabel;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure ButtonNewClick(Sender: TObject);
procedure ButtonPreviousClick(Sender: TObject);
procedure ButtonNextClick(Sender: TObject);
procedure ButtonSaveClick(Sender: TObject);
procedure CloseClick(Sender: TObject);
private
{ procedure LoadRecord;}
procedure SaveRecord;
procedure ShowRecord;
procedure ClearData;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
type
Address = record
Lastname: string[20];
Firstname: string[20];
Phone: string[15];
StreetAddress: string[50];
City: string[40];
State: string[2];
ZipCode: string[10];
end;
var
AddressFile: file of Address;
AddressData: Address;
FName: string;
RecSize, CurRec: Longint;
{procedure TForm1.LoadRecord ;
begin
Read(AddressFile,AddressData);
ShowRecord;
end;}
procedure TForm1.SaveRecord;
begin
AddressData.Lastname := Edit1.Text;
AddressData.Firstname := Edit2.Text;
AddressData.Phone := Edit3.Text;
AddressData.StreetAddress := Edit4.Text;
AddressData.City := Edit5.Text;
AddressData.State := Edit6.Text;
AddressData.ZipCode := Edit7.Text;
Write(AddressFile, AddressData);
end;
procedure TForm1.ClearData;
begin
Edit1.Text := '';
Edit2.Text := '';
Edit3.Text := '';
Edit4.Text := '';
Edit5.Text := '';
Edit6.Text := '';
Edit7.Text := '';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ClearData;
CurRec := 0;
FName := 'Address.dat';
AssignFile(AddressFile, FName);
RecSize := SizeOf(AddressData);
if FileExists(FName) then
begin
Reset(AddressFile);
if not EOF(AddressFile) then
begin
Read(AddressFile, AddressData);
ShowRecord;
end;
end
else
begin
ClearData;
Rewrite(AddressFile);
end;
end;
procedure TForm1.ButtonNewClick(Sender: TObject);
begin
// if (Edit1.Text<>'')or(Edit2.Text<>'')or(Edit3.Text<>'')or(Edit4.Text<>'')or(Edit5.Text<>'')or(Edit6.Text<>'')or(Edit7.Text<>'') then
begin
CurRec := 0;
repeat
Seek(AddressFile, CurRec);
Read(AddressFile, AddressData);
if (AddressData.Lastname = '') and
(AddressData.Firstname = '') and
(AddressData.Phone = '') and
(AddressData.StreetAddress = '') and
(AddressData.City = '') and
(AddressData.State = '') and
(AddressData.ZipCode = '') then Exit;
CurRec := CurRec + 1;
until EOF(AddressFile);
ClearData;
end;
Seek(AddressFile, CurRec);
SaveRecord;
ShowRecord
end;
procedure TForm1.ButtonPreviousClick(Sender: TObject);
begin
if CurRec - 1 < 0 then
begin
CurRec := 0;
Seek(AddressFile, CurRec);
ShowMessage('This is the begining of the file');
end
else
begin
CurRec := CurRec - 1;
Seek(AddressFile, CurRec);
Read(AddressFile, AddressData);
// Seek(AddressFile,CurRec);
ShowRecord;
end;
end;
procedure TForm1.ButtonNextClick(Sender: TObject);
begin
CurRec := CurRec + 1;
Seek(AddressFile, CurRec);
if not EOF(AddressFile) then
begin
Read(AddressFile, AddressData);
Seek(AddressFile, CurRec);
ShowRecord;
end
else
begin
CurRec := CurRec - 1;
Seek(AddressFile, CurRec);
ShowMessage('This is the end of the file');
end;
end;
procedure TForm1.ShowRecord;
begin
Seek(AddressFile, CurRec);
Read(AddressFile, AddressData);
Form1.Edit1.Text := AddressData.Lastname;
Form1.Edit2.Text := AddressData.Firstname;
Form1.Edit3.Text := AddressData.Phone;
Form1.Edit4.Text := AddressData.StreetAddress;
Form1.Edit5.Text := AddressData.City;
Form1.Edit6.Text := AddressData.State;
Form1.Edit7.Text := AddressData.ZipCode;
RecordNumber.Caption := IntToStr(CurRec);
end;
procedure TForm1.ButtonSaveClick(Sender: TObject);
begin
SaveRecord;
ShowRecord;
end;
procedure TForm1.CloseClick(Sender: TObject);
begin
SaveRecord;
CloseFile(AddressFile);
Application.Terminate;
end;
end.
object Form1: TForm1
Left = 236
Top = 110
Width = 470
Height = 370
Caption = 'Address Book'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clBlack
Font.Height = -13
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = True
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 8
Top = 12
Width = 70
Height = 13
Caption = 'Last Name:'
end
object Label2: TLabel
Left = 8
Top = 60
Width = 77
Height = 13
Caption = 'First Name:'
end
object Label3: TLabel
Left = 8
Top = 108
Width = 91
Height = 13
Caption = 'Phone Number:'
end
object Label4: TLabel
Left = 8
Top = 156
Width = 105
Height = 13
Caption = 'Street Address:'
end
object Label5: TLabel
Left = 8
Top = 204
Width = 35
Height = 13
Caption = 'City:'
end
object Label6: TLabel
Left = 176
Top = 204
Width = 35
Height = 13
Caption = 'State'
end
object Label7: TLabel
Left = 296
Top = 204
Width = 49
Height = 13
Caption = 'ZipCode'
end
object Label8: TLabel
Left = 252
Top = 24
Width = 84
Height = 20
Caption = '记录号:'
Font.Charset = GB2312_CHARSET
Font.Color = clBlue
Font.Height = -20
Font.Name = '隶书'
Font.Style = [fsBold]
ParentFont = False
end
object Image1: TImage
Left = 420
Top = 12
Width = 32
Height = 32
AutoSize = True
Picture.Data = {
055449636F6E0000010001002020100000000000E80200001600000028000000
2000000040000000010004000000000080020000000000000000000010000000
0000000000000000000080000080000000808000800000008000800080800000
C0C0C000808080000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000
FFFFFF00FFFFFFFF22F2F2FFFF22F222FFFFFFFFFFFFFFFFF22226F2F2F2222F
FFFFFFFFFFFFFFFFFFF22222F22222FF22FFFFFFFFFFFFFFFF222222F222FF22
2F22FFFFFFFFFFFFFFFFFF2222222222F22FFFFFFFFFFFFFFF26222222222222
2222222FFFFFFFFFFFFF222F2222F22222222FFFFFFFFFFFFF22222F2F222F22
F22FFFFFFFFFF22FF222F2FF2F2222222F22FFFFFFFFF226FFF2F2F22F2222FF
22FFFFFFFFFFF222FFFFFFF22F2F2222FFFFFFFFFFFFF2222FFFFF222F2F222F
FFFFFFFFFFFFF2226FFFFF2222FF2F222FFFFFFFFFFFFF222FFFF22222FFFFFF
FFFFFFFFFFFFFFF2FFFF22F2F222FFFFFFFFFFFFFFFFFFFF2FF22FF2FFFFFFFF
FFFFFFFFFFFFFFFFF222FF22FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2FFFFFFFFF
FFFFFFFFFFFFFFFFD9D9FF2FFF9D9FFFFFFFFFFFFFFFFFFDDDDDDF2F9DDDDDFF
FFFFFFFFFFFFFFF9DDFFDDDDDFFDD9FFFFFFFFFFFFFFFFFDDDFBB8D8BBFDDDFF
FFFFFFFFFFFFFFFFFDDD88D88DDDFFFFFFFFFFFFFFFFFFD9DFFBF797FBFFD9DF
FFFFFFFFFFFFFDDDFFFB88988BFFFDDDFFFFFFFFFFFFF9DDFFFDDB9BDDFFFDD9
FFFFFFFFFFFFFDDDDFDFDBBBDFDFDDDDFFFFFFFFFFFFFF99DDFDDFBFDDFDDD9F
FFFFFFFFFFFFFFFFFFFDDFFFDDFFFFFFFFFFFFFFFFFFFFFFFFF9DDFDD9FFFFFF
FFFFFFFFFFFFFFFFFFFDDDDDDDFFFFFFFFFFFFFFFFFFFFFFFFFFD9D9DFFFFFFF
FFFFFFFF00000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
00000000}
end
object Edit1: TEdit
Left = 8
Top = 32
Width = 233
Height = 21
TabOrder = 0
end
object Edit2: TEdit
Left = 8
Top = 80
Width = 233
Height = 21
TabOrder = 1
end
object Edit3: TEdit
Left = 8
Top = 128
Width = 233
Height = 21
TabOrder = 2
end
object Edit4: TEdit
Left = 8
Top = 176
Width = 445
Height = 21
TabOrder = 3
end
object Edit5: TEdit
Left = 8
Top = 220
Width = 161
Height = 21
TabOrder = 4
end
object Edit6: TEdit
Left = 176
Top = 220
Width = 113
Height = 21
TabOrder = 5
end
object Edit7: TEdit
Left = 296
Top = 220
Width = 157
Height = 21
TabOrder = 6
end
object ButtonPrevious: TButton
Left = 8
Top = 260
Width = 75
Height = 25
Caption = '&Previous'
TabOrder = 7
OnClick = ButtonPreviousClick
end
object ButtonSave: TButton
Left = 140
Top = 260
Width = 75
Height = 25
Caption = '&Save'
TabOrder = 8
OnClick = ButtonSaveClick
end
object ButtonNext: TButton
Left = 8
Top = 296
Width = 75
Height = 25
Caption = '&Next'
TabOrder = 9
OnClick = ButtonNextClick
end
object ButtonNew: TButton
Left = 140
Top = 296
Width = 75
Height = 25
Caption = '&New'
TabOrder = 10
OnClick = ButtonNewClick
end
object BitBtn1: TBitBtn
Left = 376
Top = 296
Width = 75
Height = 25
TabOrder = 11
OnClick = CloseClick
Kind = bkClose
end
object RecordNumber: TPanel
Left = 252
Top = 44
Width = 77
Height = 93
Caption = '0'
Font.Charset = GB2312_CHARSET
Font.Color = clRed
Font.Height = -56
Font.Name = '宋体'
Font.Style = [fsBold]
ParentFont = False
TabOrder = 12
end
end