0

So I'm trying to make a login for a program which simply checks through a text file for a ID(username) and a password but I get the I/O error 103 with this code and I can't figure out why. I am at a very basic level of delphi so sorry for the bad code etc

procedure TfrmLogin.btnLoginClick(Sender: TObject);
var
   ID, Password: String;
   PATLogins: TextFile;
   checkpass, checkID: String;
   correctpass, correctID: Boolean;
begin
 ID:= edtID.Text;
 Password:= edtPassword.Text;
 correctID:= False;
 correctpass:= False;
 AssignFile(PATLogins, 'PATLogins.txt');
 CloseFile(PATLogins);
 Reset(PATLogins);


 while ((NOT EOF(PATLogins)) OR (checkID = ID)) do
     begin
       ReadLn(PATLogins, checkID);
     end;
 if (checkID = ID) then
     begin
        correctID:= True;
     end;
 if EOF then
     begin
       ShowMessage('Incorrect ID');
       edtID.Clear;
     end;

 CloseFile(PATLogins);
 reset(PATLogins);


 while ((NOT EOF(PATLogins)) OR (checkpass = Password)) do
       begin
           ReadLn(PATLogins, checkpass);
       end;
 if (checkpass = Password) then
     begin
        correctpass:= True;
     end;
  if EOF then
     begin
       ShowMessage('Incorrect Password');
       edtPassword.Clear;
     end;
 CloseFile(PATLogins);

 if (correctID = True) AND (correctpass = True) then
    begin
         frmLogin.Close;
    end;

end;
Arioch 'The
  • 15,799
  • 35
  • 62
  • what is your Delphi version ? your way of reading files is obsoleted for about 20 years now – Arioch 'The Oct 17 '16 at 17:30
  • `103 File not open`: *Reported by the following functions : Close, CloseFile, Read, Write, Seek, EOf, FilePos, FileSize, Flush, BlockRead, and BlockWrite if the file is not open*. – LU RD Oct 17 '16 at 17:34
  • http://wiki.freepascal.org/TStringList-TStrings_Tutorial – Arioch 'The Oct 17 '16 at 17:37
  • Try a websearch. This has been discussed ad infinitum here and elsewhere. – David Heffernan Oct 17 '16 at 17:46
  • @Arioch'The Delphi 2010 – Angus Gregor Mclennan Pearce Oct 17 '16 at 17:47
  • @AngusGregorMclennanPearce you're supposed to specify proper tags. Then unless files are realy huge you are supposed to either load it into StringList like shown buy the above link, or into the array via TFile class as in http://stackoverflow.com/a/7277823/976391 - then you just do a standard loops `var s: string; ss: TArray; { or ss: TStringList; }; ... {loading file content into ss} for s in ss do begin ... end;` – Arioch 'The Oct 17 '16 at 18:53

1 Answers1

7

You're closing the file before it's open. AssignFile does not open any files. It merely sets up the given TextFile variable so that future file operations (like Reset or Rewrite) will know which file name to use. Remove the first CloseFile call — the one immediately after you use AssignFile.

Don't remove all the CloseFile calls because you evidently wish to process the same file twice. You need to close and re-open the file for that to work, which is what your current code already does correctly.

(You don't really want to process the file twice. Your current algorithm allows that any username or password can be provided as the username, and any password or username can be provided as the password. That means nobody even needs a password because the username can be given as the password. You need to rethink how you do your authentication, but that's beyond the scope of this question.)

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467