所以基本上我在英国上大学(所以那是美国人(17岁)的学校),我必须为我的期末考试做一个文件处理项目。我已经完成了大部分工作,但登录屏幕让我感到非常沉重。我想从程序中读取用户名和密码并登录人员。可以有 2 个不同的输出。如果您在用户名中输入"员工",在用户名中输入"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:UsersNinjaBoffinDesktopDelphiDataFilesLoginData.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:UsersNinjaBoffinDesktopDelphiDataFilesLoginData.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:UsersNinjaBoffinDesktopDelphiDataFilesLoginData.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:UsersNinjaBoffinDesktopDelphiDataFilesLoginData.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;
如果这不是用户名和密码,则必须输入老师为您设置的密码和用户名。这是在教师表格中,文件是"登录数据.txt"。
procedure TfrmAddNewUser.btnAddNewStudentClick(Sender: TObject);
begin
assignfile (LoginDataFile,'C:UsersNinjaBoffinDesktopDelphiDataFilesLoginData.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错误,我不知道它来自哪里(它在登录表单中)那么如何解决此错误呢?登录会起作用吗?
这是我第一次编程
谢谢你们解决问题(I/O错误103)原来我正在关闭不存在的文件
Closefile (StudentFile)
当它应该在
Closefile (LoginDataFile)
感谢所有帮助过的人
我的期末考试迟到了,但也许这对未来的项目有帮助。您可以使用基于名称-值对和 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;
如果需要更多数据字段,您可以完成这个实现值结构的简单基本模式。