做了个类似通讯录的小程序,用的是“有类型文件”实现了读,写可是不知道怎样删除记录?

解决方案 »

  1.   

    一个小程序,有两个按钮,分别是新增和保存。现在想增加“删除”按钮,可是怎么删除呢?
    -------------------------------------------------------------------------------unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls, StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        numberedit: TLabeledEdit;
        nameedit: TLabeledEdit;
        phoneedit: TLabeledEdit;
        addressedit: TLabeledEdit;
        choosenumber: TUpDown;
        newbutton: TButton;
        savebutton: TButton;
        genderlabel: TLabel;
        malebtn: TRadioButton;
        femalebtn: TRadioButton;
        Label1: TLabel;
        birthdayedit: TDateTimePicker;
        procedure FormCreate(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure choosenumberChanging(Sender: TObject;
          var AllowChange: Boolean);
        procedure newbuttonClick(Sender: TObject);
        procedure savebuttonClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
      type
      tfriendrec=packed record
      no:byte;
      name:string[10];
      gender:bool;
      birthday:tdatetime;
      phone:string[15];
      address:string[50];
      end;
    var
      Form1: TForm1;
       friendrec:tfriendrec;
       friendfile:file of tfriendrec;
    implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
    assignfile(friendfile,extractfilepath(application.exename)+'ylg.dat');
    reset(friendfile);
    choosenumber.Max:=filesize(friendfile)-1;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
    closefile(friendfile);
    end;procedure TForm1.choosenumberChanging(Sender: TObject;
      var AllowChange: Boolean);
    begin
    numberedit.Text:=inttostr(choosenumber.Position);
    seek(friendfile,choosenumber.Position);
    read(friendfile,friendrec);
    nameedit.Text:=friendrec.name;
    phoneedit.Text:=friendrec.phone;
    addressedit.Text:=friendrec.address;
    birthdayedit.DateTime:=friendrec.birthday;
    if friendrec.gender then
    malebtn.Checked:=true
    else
    femalebtn.Checked:=true;
    end;procedure TForm1.newbuttonClick(Sender: TObject);
    begin
    seek(friendfile,filesize(friendfile));
    choosenumber.max:=choosenumber.max+1;
    choosenumber.Position:=filesize(friendfile);
    numberedit.Text:=inttostr(filesize(friendfile));
    choosenumber.enabled:=false;
    numberedit.Enabled:=false;
    nameedit.Text:='';
    phoneedit.Text:='';
    addressedit.Text:='';
    end;procedure TForm1.savebuttonClick(Sender: TObject);
    begin
    with friendrec do
    begin
    no:=choosenumber.Position;
    name:=nameedit.Text;
    gender:=malebtn.Checked;
    birthday:=birthdayedit.DateTime;
    phone:=phoneedit.Text;
    address:=addressedit.text;
    end;
    write(friendfile,friendrec);
    choosenumber.Enabled:=true;
    numberedit.Enabled:=true;
    end;end.