// Operation example:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

where
  Form1: TForm1;

implementation

{$R *.dfm}

type
  TPersonRec = packed record
    name: string[12];
    age: Word;
    birthday: TDate;
  end ; // First define a structure

was 
  DataFile:file of TPersonRec;   // Declaration DataFile is used to read and write TPersonRec structure data
  PersonRec: TPersonRec;   // declare structure variables
  FileName: string = "c: \ temp \ test.dat";   // Files ready for operation


// Create and write a file
procedure TForm1.Button1Click(Sender: TObject);
begin 
  AssignFile (DataFile, FileName);   // Associate file
  Rewrite (DataFile);   // Create a file, overwrite if it exists

  PersonRec.name: = 'Zhang San';
  PersonRec.age := 18;
  PersonRec.birthday := StrToDate('1990-1-1');
  Write (DataFile, PersonRec);   // Write

  PersonRec.name: = 'Li Si'er;
  PersonRec.age := 81;
  PersonRec.birthday := StrToDate('1927-11-11');
  Write (DataFile, PersonRec);   // Write

  CloseFile (DataFile);   // Close
end;


// Append data
procedure TForm1.Button2Click(Sender: TObject);
begin
  AssignFile(DataFile,FileName);
  Reset (DataFile);   // Append only for text files
  Seek (DataFile, FileSize (DataFile)); // Move   to the end of the file, where FileSize indicates how many records there are

  PersonRec.name: = 'Wang Ermazi';
  PersonRec.age := 1;
  PersonRec.birthday := StrToDate('2006-12-19');
  Write(DataFile,PersonRec);

  PersonRec.name: = 'Chang';
  PersonRec.age := 2000;
  PersonRec.birthday := StrToDate('0007-7-7');
  Write(DataFile,PersonRec);

  CloseFile(DataFile);

end;


// Read the data (use Memo to display the data)
procedure TForm1.Button3Click(Sender: TObject);
begin
  AssignFile(DataFile,FileName);
  Reset(DataFile);

  Memo1.Clear;
  Read (DataFile, PersonRec);   // Read one, the pointer automatically moves to the next
  Memo1.Lines.Add(PersonRec.name);
  Memo1.Lines.Add(IntToStr(PersonRec.age));
  Memo1.Lines.Add(DateToStr(PersonRec.birthday));

  Read (DataFile, PersonRec);   // Read next
  Memo1.Lines.Add(PersonRec.name);
  Memo1.Lines.Add(IntToStr(PersonRec.age));
  Memo1.Lines.Add(DateToStr(PersonRec.birthday));

  Seek(DataFile,FileSize(DataFile)-1);   // The pointer moves to the best one
  Read(DataFile,PersonRec);
  Memo1.Lines.Add(PersonRec.name);
  Memo1.Lines.Add(IntToStr(PersonRec.age));
  Memo1.Lines.Add(DateToStr(PersonRec.birthday));

  CloseFile(DataFile);

{show result:
  Zhang San
  18
  1990-1-1
  Li Sier
  81
  1927-11-11
  Chang'e
  2000
  0007-7-7
}
end;


//change the data
procedure TForm1.Button4Click(Sender: TObject);
begin
  AssignFile(DataFile,FileName);
  Reset(DataFile);

  Seek(DataFile,3);   // The pointer moves to the fourth item
  Read (DataFile, PersonRec);   // Read
  PersonRec.age := 0;
  PersonRec.birthday := StrToDate('2007-10-24');

  Seek(DataFile,3);   // The pointer moves to the fourth item
  Write (DataFile, PersonRec);   // Write

  CloseFile(DataFile);
end;


// Read all data
procedure TForm1.Button5Click(Sender: TObject);
begin
  AssignFile(DataFile,FileName);
  Reset(DataFile);

  Memo1.Clear;
  while not Eof(DataFile) do
  begin
    Memo1.Lines.Add('Article' + IntToStr(FilePos(DataFile)+1) + 'Article:');
    Read(DataFile,PersonRec);
    Memo1.Lines.Add(PersonRec.name);
    Memo1.Lines.Add(IntToStr(PersonRec.age));
    Memo1.Lines.Add(DateToStr(PersonRec.birthday));
    Memo1.Lines.Add('');   // come a blank line
  end;

  CloseFile(DataFile);

{show result:
  Article 1:
  Zhang San
  18
  1990-1-1

  Article 2:
  Li Sier
  81
  1927-11-11

  Article 3:
  Wang Ermazi
  1
  2006-12-19

  Article 4:
  Chang'e
  0
  2007-10-24
}

end;

end.