文件型(*.cfg)密码文件未先设定出错,如何容错?(50分)

  • 主题发起人 主题发起人 zl2826
  • 开始时间 开始时间
Z

zl2826

Unregistered / Unconfirmed
GUEST, unregistred user!
很菜的问题,业余学习书本范例,出错代码如下//???处,请指教!unit Unit100;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ExtCtrls,Unit102,Unit103;

type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
Panel1: TPanel;
Label1: TLabel;
Edit1: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.BitBtn1Click(Sender: TObject);
var
pwdfile:textfile;
pwdstr:string;
times:integer;
begin
if checkbox1.Checked=true then
assignfile(pwdfile,'c:/work/管理员.cfg')
else
assignfile(pwdfile,'c:/work/学生.cfg');
reset(pwdfile); //?????
readln(pwdfile,pwdstr);
closefile(pwdfile);
times:= times+1;
if edit1.Text=pwdstr then
begin
if checkbox1.Checked=true then form2.show
else form3.show;
end
else
begin
if MessageDlg('密码输入错误。是否退出?',mtConfirMation,[mbYes,mbNo],0)=mrYes then Close
else if times<3 then edit1.SetFocus
else
begin
MessageDlg('对不起,密码输入严重错误!请退出。',mtInformation,[mbOk],0);
Application.Terminate;
end;
end;
end;
end.
 
把原来的
assignfile(pwdfile,FileName)
改为
if not FileExists(FileName) then
FileClose(FileCreate(FileName));
 
请教PXie:改后仍错?提错在//处
unit Unit100;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ExtCtrls,Unit102,Unit103;

type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
Panel1: TPanel;
Label1: TLabel;
Edit1: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.BitBtn1Click(Sender: TObject);
var
pwdfile:textfile;
pwdstr:string;
times:integer;

//原来的
//assignfile(pwdfile,FileName)
//改为
// if not FileExists(FileName) then
// FileClose(FileCreate(FileName));

begin
if checkbox1.Checked=true then
if FileExists('c:/work/管理员.cfg') then
FileClose(FileCreate('c:/work/管理员.cfg'))
//else 不加//提示 'File not found'
//assignfile(pwdfile,'c:/work/管理员.cfg') 加//提示 'I/O error 102'
else
if FileExists('c:/work/学生.cfg') then
FileClose(FileCreate('c:/work/学生.cfg')) ;
//else
//assignfile(pwdfile,'c:/work/学生.cfg');
reset(pwdfile);
readln(pwdfile,pwdstr);
closefile(pwdfile);
times:= times+1;
if edit1.Text=pwdstr then
begin
if checkbox1.Checked=true then form2.show
else form3.show;
end
else
begin
if MessageDlg('密码输入错误。是否退出?',mtConfirMation,[mbYes,mbNo],0)=mrYes then Close
else if times<3 then edit1.SetFocus
else
begin
MessageDlg('对不起,密码输入严重错误!请退出。',mtInformation,[mbOk],0);
Application.Terminate;
end;
end;
end;
end.
 
在Pxie的提示下,自己已解决,为了鼓励他,50分归他,请Pxie发 email联系!交个朋友!
 
后退
顶部