注册 登录
编程论坛 Delphi论坛

[求助]如何新建一个.dat文件并写入数据

cch8080 发布于 2007-04-29 08:50, 6664 次点击

我想通过键盘输入数据,将包括学号,姓名,数学,语文,电子等学生成绩数据输入到一个顺序文件中(stu.dat)。下面是代码,请大家帮忙看下错在哪,什么只能新建,但没能把输入的数据保存到文件中,请高手们帮忙该该,我在这先谢谢大家了!!
unit Unit4;

interface

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

type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;

procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
type
studentrecord=record
xh,xm:string;
xb:string;
sx,yy,dz:string;
end;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var t:studentrecord;
f:file;

begin

with t do
begin
xh:=edit1.Text;
xm:=edit2.text;
if radiobutton1.Checked=true then
xb:='男'
else
xb:='女';
sx:=edit3.text;
yy:=edit4.text;
dz:=edit5.text;
end;
assignfile(f,'e:/stu.dat');
rewrite(f);
closefile(f);
end;


end.

6 回复
#2
phonbob2007-04-29 10:24
file of
#3
cch80802007-04-29 13:44

不行啊,要是那样写的话,他就出现下面的错误提示:
[error]Unit4.pas(49):Type expected but';'found

[Fatal Error]Project4.dpr(5);Could not compile used unit'Unit4.pas'

#4
phonbob2007-04-29 13:53

没写;号

#5
anthony6342007-04-29 14:28

assignfile(f,'e:/stu.dat');
rewrite(f);
closefile(f);
写入的语句怎么没有?
f:file of studentrecord;


assignfile(f,'e:/stu.dat');
rewrite(f);
try
write(f,t);
finally
closefile(f);
end;

#6
cch80802007-04-30 07:59
什么还是不行啊,那样写了,可还是出现了下面的错误提示:[Error]Unit4.pas(49):Type 'studentrecord' needs finalization.not allowed in file type

[fatal Error]Project4.dpr(5):Could not compile used unit'Unit4.pas'
#7
zhpseason2007-08-31 20:00

由于string类型不是delphi中的基本数据类型,string实际上是一个类,故出现 楼上的提示错误,可以将string类型,改为array of char数据,则没有问题

1