Recent

Author Topic: [SOLVED] Sigsegv Exception On button click! / Login system  (Read 3724 times)

Syntax

  • New Member
  • *
  • Posts: 23
[SOLVED] Sigsegv Exception On button click! / Login system
« on: November 27, 2015, 11:21:30 am »
Whenever I run this it works, but then it throws the error as soon as input a username and click "login" button, I get the error from sigsegv 004ae3f and it opens the assember, no idea how to fix. Any help, with the login screen as well would be appreciated, thanks!

(Entire unit code)
Code: Pascal  [Select][+][-]
  1. unit Unit9;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TFormLogin }
  14.  
  15.   TFormLogin = class(TForm)
  16.     ButtonLogin: TButton;
  17.     LabelLogin: TLabel;
  18.     LabelTest: TLabel;
  19.     LEditPassword: TLabeledEdit;
  20.     LEditUsername: TLabeledEdit;
  21.     procedure ButtonLoginClick(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.     procedure CheckUsername;
  25.     procedure CheckPassword;
  26.   public
  27.     { public declarations }
  28.   end;
  29.  
  30. var
  31.   FormLogin: TFormLogin;
  32.   Username:array of string;
  33.   Password:array of string;
  34.   UsernameFile:textfile;
  35.   PasswordFile:textfile;
  36.   FileExists:integer;
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41.  
  42. uses
  43.   Unit1, Unit2;
  44.  
  45. procedure TFormLogin.ButtonLoginClick(Sender: TObject);
  46. begin
  47.    CheckUsername();
  48. end;
  49.  
  50. procedure TFormLogin.CheckUsername;
  51. var
  52.   Linenumber:integer;
  53. begin
  54.    linenumber:=0;
  55.       assignfile(UsernameFile, 'UsernameFile.txt');
  56.   try
  57.     reset(UsernameFile);
  58.       while not eof(UsernameFile) do
  59.          begin
  60.       linenumber:=linenumber+1; //LineNumber = 1,2,3,4 etc.
  61.      // readln(UsernameFile,Username[linenumber]); // Read into array
  62.     {  IF Username[linenumber] = LEditusername.Text THEN
  63.       begin
  64.  
  65.       end;
  66.              }
  67.  
  68.       readln(UsernameFile);
  69.  
  70.  
  71.           end;
  72.       CloseFile(UsernameFile);
  73.     except
  74.     showmessage('File Error'); //Dialog box
  75.  
  76. end;
  77.     setlength(username,linenumber);
  78.        assignfile(UsernameFile, 'UsernameFile.txt');
  79.   try
  80.     reset(UsernameFile);
  81.     while not eof(Usernamefile) do
  82.     begin
  83.       readln(UsernameFile,Username[linenumber]); // Read into array
  84.       IF Username[linenumber] = LEditusername.Text THEN
  85.       begin
  86.          Checkpassword();
  87.       end;
  88.  
  89.     end;
  90.          CloseFile(UsernameFile);
  91.    except
  92.     showmessage('File Error'); //Dialog box
  93.  
  94. end;
  95.  
  96. end;
  97.  
  98. procedure TFormLogin.CheckPassword;     // still in dev
  99. var
  100. Linenumber1:integer;
  101. begin
  102.   linenumber1:=0;
  103.     assignfile(PasswordFile, 'PasswordFile.txt');
  104.   try
  105.     reset(PasswordFile);
  106.     while not eof(PasswordFile) do
  107.     begin
  108.       linenumber1:=linenumber1+1;
  109.       readln(PasswordFile, Password[linenumber1]);
  110.       IF LEditPassword.Text = Password[linenumber1] THEN
  111.       begin
  112.         showmessage('Correct');
  113.         FormLogin.Visible:=False;
  114.         FormMenu.Showmodal;
  115.       end
  116.       else
  117.       showmessage('Incorrect');
  118.  
  119.     end;
  120.  
  121.     closefile(Passwordfile);
  122.   except
  123.     ShowMessage('File Error - Please Check Your File');
  124.   end;
  125.  
  126. end;
  127. end.
« Last Edit: November 27, 2015, 01:55:07 pm by Syntax »

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Sigsegv Exception On button click!
« Reply #1 on: November 27, 2015, 11:54:58 am »
In the first part of "CheckUserName" you are counting the lines contained in the usernames file. But you never use this information to set the length of the UserName array. You also do not dimension the array Password

BTW, unless you don't need this anywhere else, it is not necessary to store all the usernames and passwords in an array. Just declare a local variable for the string currently read from the file and compare it with the username/password in the edit:
Code: Pascal  [Select][+][-]
  1. procedure TFormLogin.CheckUsername;
  2. var
  3.   s: String;
  4. begin
  5.   assignfile(UsernameFile, 'UsernameFile.txt');
  6.   try
  7.     reset(UsernameFile);
  8.     while not eof(UsernameFile) do
  9.     begin
  10.       ReadLine(s);
  11.       if s = LEditUsername.Text then
  12.         Checkpassword();
  13.     end;
  14.     CloseFile(UsernameFile);
  15.   except
  16.     showmessage('File Error'); //Dialog box
  17.   end;
  18. end;

balazsszekely

  • Guest
Re: Sigsegv Exception On button click!
« Reply #2 on: November 27, 2015, 11:56:09 am »
Forget that old pascal style AssigFile/ReadFile etc...Just read to file into a TStringList:
Code: Pascal  [Select][+][-]
  1. var
  2.   SL: TStringList;
  3.   I: Integer;
  4. begin
  5.   SL := TStringList.Create;
  6.   try
  7.     SL.LoadFromFile('c:\text.txt');
  8.     for I := 0 to Sl.Count - 1  do
  9.     begin
  10.       //do something with each line
  11.       ShowMessage(SL.Strings[I]);
  12.     end;
  13.   finally
  14.     SL.Free;
  15.   end;
  16. end;

Later you can save it back to file, like this:
Code: Pascal  [Select][+][-]
  1. SL.SaveToFile('c:\text_new.txt');

Syntax

  • New Member
  • *
  • Posts: 23
Re: Sigsegv Exception On button click!
« Reply #3 on: November 27, 2015, 12:27:23 pm »
In the first part of "CheckUserName" you are counting the lines contained in the usernames file. But you never use this information to set the length of the UserName array. You also do not dimension the array Password

BTW, unless you don't need this anywhere else, it is not necessary to store all the usernames and passwords in an array. Just declare a local variable for the string currently read from the file and compare it with the username/password in the edit:
Code: Pascal  [Select][+][-]
  1. procedure TFormLogin.CheckUsername;
  2. var
  3.   s: String;
  4. begin
  5.   assignfile(UsernameFile, 'UsernameFile.txt');
  6.   try
  7.     reset(UsernameFile);
  8.     while not eof(UsernameFile) do
  9.     begin
  10.       ReadLine(s);
  11.       if s = LEditUsername.Text then
  12.         Checkpassword();
  13.     end;
  14.     CloseFile(UsernameFile);
  15.   except
  16.     showmessage('File Error'); //Dialog box
  17.   end;
  18. end;

I need to check the username against the password, my idea behind the reading amount of lines was to get the line number that the username is on, and then check that line number on passwordfile against the user input, if they both match, let them log in.

Would this work the same with following this method?

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Sigsegv Exception On button click! / Login system
« Reply #4 on: November 27, 2015, 01:04:34 pm »
You don't need all the lines to verify username and password. Just do the check on the fly while reading the file and count the line number. When you have a matching username pass this linenumber to the CheckPassword method. Make this one a function which returns true or false for "password correct" or not. After you found the username exit reading the user name file; it does not matter if the password matches or not - there won't be another user with the same name.

Code: Pascal  [Select][+][-]
  1. procedure TFormLogin.CheckUsername;
  2. var
  3.   s: String;
  4.   linenumber: Integer;
  5.   correct: Boolean;
  6. begin
  7.   correct := false;
  8.   assignfile(UsernameFile, 'UsernameFile.txt');
  9.   try
  10.     while not eof(UsernameFile) do
  11.     begin
  12.       ReadLine(s);
  13.       if s = LEditUsername.Text then begin
  14.         correct := Checkpassword(linenumber);
  15.         break;  // no need to continue search once a user has been found!
  16.       end;
  17.       inc(linenumber);
  18.     end;
  19.     CloseFile(Usernamefile);
  20.     if correct then
  21.     begin
  22.       ShowMessage('Correct.');
  23.       Close;  // Close the login form if correct
  24.       FormMenu.Show;  // Is this the main form? If yes it is modal anyway!
  25.     end else
  26.       // not correct. Let user decide whether to leave the form open for another try or terminate the Application.
  27.       if MessageDlg('Incorrect username or password. Retry or shutdown application?', mtInformation, [mbRetry, mbClose], 0) = mrClose then halt;
  28.   except
  29.     showmessage('File Error'); //Dialog box
  30.   end;
  31. end;
  32.  
  33. function TFormLogin.CheckPassword(UserInLineNumber: Integer): Boolean;
  34. var
  35.   s: String;
  36.   linenumber: Integer;
  37. begin
  38.   result := false;
  39.   linennumber := 0;
  40.   assignfile(PasswordFile, 'PasswordFile.txt');
  41.   try
  42.     reset(PasswordFile);
  43.     while not eof(PasswordFile) do
  44.     begin
  45.       readln(PasswordFile, s);
  46.       if (linenumber = UserInLineNumber) then
  47.       begin
  48.         result := (LEditPassword.Text = s);
  49.         break;
  50.       end;
  51.       inc(linenumber);
  52.     end;
  53.     CloseFile(Passwordfile);
  54.   except
  55.     ShowMessage('File Error - Please Check Your File');
  56.   end;
  57. end;
  58.  

Syntax

  • New Member
  • *
  • Posts: 23
Re: Sigsegv Exception On button click! / Login system
« Reply #5 on: November 27, 2015, 01:30:41 pm »
You don't need all the lines to verify username and password. Just do the check on the fly while reading the file and count the line number. When you have a matching username pass this linenumber to the CheckPassword method. Make this one a function which returns true or false for "password correct" or not. After you found the username exit reading the user name file; it does not matter if the password matches or not - there won't be another user with the same name.

Code: Pascal  [Select][+][-]
  1. procedure TFormLogin.CheckUsername;
  2. var
  3.   s: String;
  4.   linenumber: Integer;
  5.   correct: Boolean;
  6. begin
  7.   correct := false;
  8.   assignfile(UsernameFile, 'UsernameFile.txt');
  9.   try
  10.     while not eof(UsernameFile) do
  11.     begin
  12.       ReadLine(s);
  13.       if s = LEditUsername.Text then begin
  14.         correct := Checkpassword(linenumber);
  15.         break;  // no need to continue search once a user has been found!
  16.       end;
  17.       inc(linenumber);
  18.     end;
  19.     CloseFile(Usernamefile);
  20.     if correct then
  21.     begin
  22.       ShowMessage('Correct.');
  23.       Close;  // Close the login form if correct
  24.       FormMenu.Show;  // Is this the main form? If yes it is modal anyway!
  25.     end else
  26.       // not correct. Let user decide whether to leave the form open for another try or terminate the Application.
  27.       if MessageDlg('Incorrect username or password. Retry or shutdown application?', mtInformation, [mbRetry, mbClose], 0) = mrClose then halt;
  28.   except
  29.     showmessage('File Error'); //Dialog box
  30.   end;
  31. end;
  32.  
  33. function TFormLogin.CheckPassword(UserInLineNumber: Integer): Boolean;
  34. var
  35.   s: String;
  36.   linenumber: Integer;
  37. begin
  38.   result := false;
  39.   linennumber := 0;
  40.   assignfile(PasswordFile, 'PasswordFile.txt');
  41.   try
  42.     reset(PasswordFile);
  43.     while not eof(PasswordFile) do
  44.     begin
  45.       readln(PasswordFile, s);
  46.       if (linenumber = UserInLineNumber) then
  47.       begin
  48.         result := (LEditPassword.Text = s);
  49.         break;
  50.       end;
  51.       inc(linenumber);
  52.     end;
  53.     CloseFile(Passwordfile);
  54.   except
  55.     ShowMessage('File Error - Please Check Your File');
  56.   end;
  57. end;
  58.  

http://i.imgur.com/whX9txC.png error on the code

Code: Pascal  [Select][+][-]
  1.       ReadLn(s);
  2.       if s = LEditUsername.Text then begin
  3.         correct := Checkpassword(linenumber);
  4.         break;  // no need to continue search once a user has been found!
  5.       end;
  6.       inc(linenumber); //LINE 69 - WHERE ERROR OCCURS
  7.     end;
  8.     CloseFile(Usernamefile);  

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Sigsegv Exception On button click! / Login system
« Reply #6 on: November 27, 2015, 01:35:23 pm »
I see now that there are a few bugs in my (untested) code. But anyway - you seem to be a fan of the old-fashioned file routines which you applied correctly in your first post. Therefore I am sure you will find what's wrong with my "Readln(s)", and you will also find out if the files are correctly opened...

Syntax

  • New Member
  • *
  • Posts: 23
Re: Sigsegv Exception On button click! / Login system
« Reply #7 on: November 27, 2015, 01:42:30 pm »
I see now that there are a few bugs in my (untested) code. But anyway - you seem to be a fan of the old-fashioned file routines which you applied correctly in your first post. Therefore I am sure you will find what's wrong with my "Readln(s)", and you will also find out if the files are correctly opened...

Yep, fixed all of the errors, only thing throwing it off now is the fact that even if i put in the CORRECT information, it throws "Incorrect password..."


Got it to work, typo my end!

 

TinyPortal © 2005-2018