How do I solve I/O error 103 in delphi
Solution 1
Thank You Guys for solving the problem (I/O error 103) Turns out i was closing file that didn't exist
Closefile (StudentFile)
When it should have been
Closefile (LoginDataFile)
Thanks to everyone who helped
Solution 2
I'm late for your finals, but maybe this can help for future projects. You can use an approach based on name-values pairs and on TStringLIst object ability to load and save text files, so you don't have to care about files handles:
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;
You can complete this simple basic pattern implementing a value structure if more data field are required.
NinjaBoffin
Updated on May 12, 2020Comments
-
NinjaBoffin about 4 years
So Basically I'm in College in the UK (So that's school for Americans (17 years old)) and I have to make a file handling project for my finals. I have done most of it but it's the login screen that is weighing me down a lot. What I want from the program to do is read the username and password and log the people in. There can be 2 different outputs. If you type in "Staff" in the username and "warwickschool2013" in the username, the program takes you to the "teacher" side of the program where they can add in new users.
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;
If that is not the username and password you have to enter the password and username made by the teacher for you. This is in the teachers form and the file is "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'm getting a constant I/O 103 error and I don't know where it's from (It's in the login form) So how do I fix this error? and will the login work?
This is my first time programming