Z
zlx05
Unregistered / Unconfirmed
GUEST, unregistred user!
我在编制一个程序,定义了一个结构文件,需要不断地添加数据,但我不能实现,基本结构如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
tdata=record
name:string[10];
num:integer;
end;
tfile=file of tdata;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Panel1: TPanel;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Panel2: TPanel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
{ function form1.getdata: tdata;}
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
f:tfile;
implementation
{$R *.DFM}
function getdata:tdata;
begin
try
with result do begin
name:=form1.edit1.text;
num:=strtoint(form1.edit2.text)
end
except
showmessage('error');abort
end
end;
procedure setdata(const data:tdata);
begin
with data do begin
form1.edit1.text:=name;
form1.edit2.text:=inttostr(num);
end
end;
procedure TForm1.Button1Click(Sender: TObject);
var
adata:tdata;
begin
if button1.caption='add' then begin
panel2.Visible:=false;
button1.caption:='save';
end
else begin
adata:=getdata;
assignfile(f,'f:/zlx/delphi/lucky/lucky.dat');
rewrite(f); {用这句会将原文件覆盖,在文件中只有一个数据,请问应如何解决}
write(f,adata);
edit1.text:='';
edit2.text:='';
button1.caption:='add';
panel2.visible:=true;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
adata:tdata;
begin
panel2.visible:=false;
assignfile(f,'f:/zlx/delphi/lucky/lucky.dat');
reset(f);
read(f,adata);
setdata(adata);
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
tdata=record
name:string[10];
num:integer;
end;
tfile=file of tdata;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Panel1: TPanel;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Panel2: TPanel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
{ function form1.getdata: tdata;}
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
f:tfile;
implementation
{$R *.DFM}
function getdata:tdata;
begin
try
with result do begin
name:=form1.edit1.text;
num:=strtoint(form1.edit2.text)
end
except
showmessage('error');abort
end
end;
procedure setdata(const data:tdata);
begin
with data do begin
form1.edit1.text:=name;
form1.edit2.text:=inttostr(num);
end
end;
procedure TForm1.Button1Click(Sender: TObject);
var
adata:tdata;
begin
if button1.caption='add' then begin
panel2.Visible:=false;
button1.caption:='save';
end
else begin
adata:=getdata;
assignfile(f,'f:/zlx/delphi/lucky/lucky.dat');
rewrite(f); {用这句会将原文件覆盖,在文件中只有一个数据,请问应如何解决}
write(f,adata);
edit1.text:='';
edit2.text:='';
button1.caption:='add';
panel2.visible:=true;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
adata:tdata;
begin
panel2.visible:=false;
assignfile(f,'f:/zlx/delphi/lucky/lucky.dat');
reset(f);
read(f,adata);
setdata(adata);
end;
end.