如何在delphi中解决I / O错误103

时间:2014-02-25 21:46:02

标签: delphi delphi-xe5

所以基本上我在英国的大学(所以这是美国人的学校(17岁))我必须为我的决赛做一个文件处理项目。 我已经完成了大部分工作但是登录屏幕让我失望了很多。我想从程序中做的是读取用户名和密码并将人员记录下来。可以有2种不同的输出。如果您在用户名中键入“Staff”,在用户名中键入“warwickschool2013”​​,程序会将您带到程序的“教师”一侧,他们可以在这里添加新用户。

unit LoginScreen;
interface

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

type
  TfrmLogin = class(TForm)
    lblWelcomeLogin: TLabel;
    edtPassword: TEdit;
    lblPassword: TLabel;
    btnLogin: TButton;
    btnClose: TButton;
    lblCounter: TLabel;
    lblAttempts: TLabel;
    Image1: TImage;
    edtUsername: TEdit;
    lblUserName: TLabel;
    procedure btnCloseClick(Sender: TObject);
    procedure btnLoginClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmLogin: TfrmLogin;


implementation
uses MainMenu, TeachersMainMenu, ViewEditTheData, Globals_Unit;
var
LoginDataArray: array of TLoginData;
counter: integer;
Password, username : String;
Count1, Count2 : integer;
Temp: TLoginData;

{$R *.dfm}

procedure TfrmLogin.btnLoginClick(Sender: TObject);
var
SearchName : string[10];
SearchCode : string[3];
begin
  Password:=edtPassword.Text;
  UserName:=edtUserName.Text;
  btnclose.enabled:= true;
 if (Password = 'warwickschool2013') AND (Username = 'Staff') then
   begin
     edtPassword.text :='';
     frmTeachersMainMenu.show;
     frmLogin.hide;
     counter:= 0;
     lblCounter.caption := IntToStr(Counter);
  end
  else
   begin
     assignfile      (LoginDatafile,'C:\Users\NinjaBoffin\Desktop\Delphi\DataFiles\LoginData.txt');
     reset (LoginDatafile);
     LoginDatacounter := FileSize(LoginDatafile);
     SetLength(LoginDataArray, LoginDatacounter);
     edtPassword.clear;
     edtUserName.Clear;
     for Count1 := 1 to LoginDataCounter  do
       begin
          read(LoginDatafile,SingleLoginData);
          LoginDataArray[Count1] := SingleLoginData;
       end;
      closefile (LoginDatafile);

      //bubble sort
      edtUserName.Clear;
      for count1 := 1 to LoginDatacounter-1 do
         for count2 := 1 to LoginDatacounter-1 do
            if LoginDataArray[count2].UserName > LoginDataArray[count2+1].UserName then
           begin
             temp := LoginDataArray[count2+1];
             LoginDataArray[count2+1] := LoginDataArray[count2];
             LoginDataArray[count2] := temp;
           end;
//Linear Search (files getting ready)
begin
  SearchName := edtUserName.text;
  assignfile     (LoginDatafile,'C:\Users\NinjaBoffin\Desktop\Delphi\DataFiles\LoginData.txt');
  reset (LoginDatafile);
  LoginDatacounter := FileSize(LoginDatafile);
  SetLength(LoginDataArray, LoginDatacounter);
  edtUserName.Clear;
  //Copying to array
  for  Count1 := 1 to StudentCounter do
   begin
      read(LoginDatafile,SingleLoginData);
      LoginDataArray[Count1] := SingleLoginData;
   end;
  closefile (Studentfile);
 //The actual linear search
for  Count1 := 1 to StudentCounter do
 begin
   if LoginDataArray[Count1].Username = SearchName then
    begin
      assignfile    (LoginDatafile,'C:\Users\NinjaBoffin\Desktop\Delphi\DataFiles\LoginData.txt');
      reset (LoginDatafile);
      LoginDatacounter := FileSize(LoginDatafile);
      SetLength(LoginDataArray, LoginDatacounter);
      edtPassword.clear;
      for Count1 := 1 to LoginDataCounter  do
        begin
          read(LoginDatafile,SingleLoginData);
          LoginDataArray[Count1] := SingleLoginData;
        end;
        closefile (LoginDatafile);
        //bubble sort
       edtUserName.Clear;
       for count1 := 1 to LoginDatacounter-1 do
         for count2 := 1 to LoginDatacounter-1 do
             if LoginDataArray[count2].Password > LoginDataArray[count2+1].Password then
                begin
                  temp := LoginDataArray[count2+1];
                  LoginDataArray[count2+1] := LoginDataArray[count2];
                  LoginDataArray[count2] := temp;
                end;
   //Linear Search (files getting ready)
   begin
      SearchName := edtPassword.text;
      assignfile (LoginDatafile,'C:\Users\NinjaBoffin\Desktop\Delphi\DataFiles\LoginData.txt');
      reset (LoginDatafile);
      LoginDatacounter := FileSize(LoginDatafile);
      SetLength(LoginDataArray, LoginDatacounter);
      edtPassword.clear;
      //Copying to array
      for  Count1 := 1 to StudentCounter do
       begin
        read(LoginDatafile,SingleLoginData);
        LoginDataArray[Count1] := SingleLoginData;
       end;
       closefile (Studentfile);
      //The actual linear search
       for  Count1 := 1 to StudentCounter do
         begin
           if LoginDataArray[Count1].Password = SearchName then
             begin
               frmLogin.Hide;
                frmMainmenu.Show;
             end;
          end;
        end;
   end;
  end;
 end;
 end;
end;

如果这不是用户名和密码,则必须输入教师为您提供的密码和用户名。这是教师形式,文件是“Logindata.txt”。

procedure TfrmAddNewUser.btnAddNewStudentClick(Sender: TObject);
begin
  assignfile (LoginDataFile,'C:\Users\NinjaBoffin\Desktop\Delphi\DataFiles\LoginData.txt');
  reset (loginDataFile);
  LoginDataCounter := FileSize(LoginDataFile);
  SingleLoginData.Username := edtNewUsername.text;
  SingleLoginData.Password := edtNewPassword.Text;
  //find where to store the record
  seek (LoginDataFile,LoginDataCounter);
  {...and put the record in}
  write(LoginDataFile,SingleLoginData);
  closefile (LoginDataFile);
  edtNewUserName.Text := '';
  edtNewPassword.Text := '';
end;

我收到了一个持续的I / O 103错误,我不知道它来自哪里(它在登录表单中) 那么我该如何解决这个错误呢?登录会起作用吗?

这是我第一次编程

2 个答案:

答案 0 :(得分:3)

感谢你们解决问题(I / O错误103) 原来我正在关闭不存在的文件

Closefile (StudentFile)

什么时候应该

Closefile (LoginDataFile)

感谢所有帮助过的人

答案 1 :(得分:1)

我的决赛迟到了,但也许这对未来的项目有帮助。 您可以使用基于名称 - 值对的方法和TStringLIst对象加​​载和保存文本文件的方法,因此您不必关心文件句柄:

const
  MyFileWithPath='c:\.......\UserList.txt';

procedure AddUser(UserId,UserPwd: string);
var
  l: TStringList;
begin
  l := TStringList.Create;
  try
    if FileExists(MyFileWithPath) then
      l.LoadFormFile(MyFileWithPath);
    l.Add(Format('%s=%s',[UserId,UserPwd]));
    l.SaveToFile(MyFileWithPath);
  finally
    l.Free;
  end;
end;

function GetUserPwd(UserId: string): string;
var
  l: TStringList;
begin
  l := TStringList.Create;
  try
    l.LoadFormFile(MyFileWithPath);
    Result := l.Values[UserId];
  finally
    l.Free;
  end;
end;

如果需要更多数据字段,您可以完成实现值结构的简单基本模式。