小弟刚工作,学习DELPHI第一周,想请要教的是我通过键盘将学生记录添加到文件.dat文件中,原意是如果原文件存在,就打开添加,如果不存在,再建立然后添加,可是每次运行都是按照不存在处理的,都是新建个文件.dat,然后进行操作。代码如下
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Button1: TButton;
Button2: TButton;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
type
studentrecord = record //students information
xh,xm,xb: string[6]; //students numbers,students names,students sexes
yy,sx,wl: Integer; //students english record,math record,physical record
end;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
t: studentrecord;
f: file of studentrecord;
size: integer;
begin
assignfile(f,'E:\delphipractise\文件.dat');
reset(f);
t.xh := edit1.Text;
t.xm := edit2.Text;
if radiobutton1.Checked = true then
t.xb := radiobutton1.Caption
else
t.xb := radiobutton2.Caption;
t.yy := strtoint(edit3.Text);
t.sx := strtoint(edit4.Text);
t.wl := strtoint(edit5.Text);
size := filesize(f);
seek(f,size);
write(f,t);
seek(f,0);
edit1.Text := '';
edit2.Text := '';
edit3.Text := '';
edit4.Text := '';
edit5.Text := '';end;procedure TForm1.Button2Click(Sender: TObject);
begin
close
end;procedure TForm1.FormCreate(Sender: TObject);
var
f: file of studentrecord;
begin
assignfile(f,'E:\delphipractise\文件.dat');
if fileexists('E:\delphipratise\文件.dat') then
reset(f)
else
rewrite(f);end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Button1: TButton;
Button2: TButton;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
type
studentrecord = record //students information
xh,xm,xb: string[6]; //students numbers,students names,students sexes
yy,sx,wl: Integer; //students english record,math record,physical record
end;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
t: studentrecord;
f: file of studentrecord;
size: integer;
begin
assignfile(f,'E:\delphipractise\文件.dat');
reset(f);
t.xh := edit1.Text;
t.xm := edit2.Text;
if radiobutton1.Checked = true then
t.xb := radiobutton1.Caption
else
t.xb := radiobutton2.Caption;
t.yy := strtoint(edit3.Text);
t.sx := strtoint(edit4.Text);
t.wl := strtoint(edit5.Text);
size := filesize(f);
seek(f,size);
write(f,t);
seek(f,0);
edit1.Text := '';
edit2.Text := '';
edit3.Text := '';
edit4.Text := '';
edit5.Text := '';end;procedure TForm1.Button2Click(Sender: TObject);
begin
close
end;procedure TForm1.FormCreate(Sender: TObject);
var
f: file of studentrecord;
begin
assignfile(f,'E:\delphipractise\文件.dat');
if fileexists('E:\delphipratise\文件.dat') then
reset(f)
else
rewrite(f);end;end.
解决方案 »
- ReplaceStr效率太低,哪儿有好的高效的字符串替换函数?
- 关于DBGrideh的双页脚
- 【潇洒】
- 求 GetCurrentDir 的用法.
- 在线等待你的答案关于单机版的ACCESS的问题
- 组播也是广播? 使用SetSockOpt接口实现的组播好象也是广播!
- 关于Access数据库的一个问题!散分啦
- 如何控制ListView中某一行的背景颜色,请高手指教。
- 怎样在一个SQL数据表的一个WORD字段(是一个WORD文档)中插入一个EXCEL表格?
- dbgrid中选中一个记录,怎么返回哪个记录的相应字段的内容阿?比如选中id为2的记录,怎么把2返回阿?
- 判断语句 输入正确还是弹出来message信息
- 两个月没上CSDN,都成富农了,**庆祝国庆**散点分表表心意
procedure TForm1.FormCreate(Sender: TObject);
var
f: file of studentrecord;
begin
if not fileexists('E:\delphipratise\文件.dat') then
begin
assignfile(f,'E:\delphipractise\文件.dat');
reWrite(f);
CloseFile(f);
end;
end;
此外,文件使用后,需要进行关闭: CloseFile函数。