使用二进制文件的用户名和登录系统
username and login system using binary files
>用户名和密码系统,使用登录功能存储帐户信息并使用功能登录登录。由于某种原因,程序在登录时读取二进制文件,但无法识别给定的名字,姓氏和密码是否与文件中的任何数据匹配。
int userno = 0;
bool check;
char firstname[10],lastname[10],password[10];
class user
{
public:
int uid;
char fname[20],lname[20],pword[20];
int age;
}u[100];
getuserno()
{
fstream f("user.dat",ios::in);
for(int i=0 ; !f.eof() ; i++)
{
f.read((char*)&u[i],sizeof(u[i]));
userno = i;
}
}
'
int signin()
{
ofstream entry("user.dat",ios::out|ios::app|ios::binary);
if(!entry) {
cout << "Cannot open file!" << endl;
return 1;
}
cout<<"Enter first namen";
cin>>u[userno].fname;
cout<<"Enter last namen";
cin>>u[userno].lname;
cout<<"Enter your passwordn";
cin>>u[userno].pword;
cout<<"Enter your agen";
cin>>u[userno].age;
if(u[userno].age<18)
{
cout<<"You must be 18 or older"<<endl;
cin>>u[userno].age;
}
u[userno].uid = userno;
entry.write((char*)&u[userno],sizeof(u[userno]));
if(!entry.good()) {
cout << "Error occurred at writing time!" << endl;
system("pause");
}
userno++;
entry.close();
system("cls");
}
'
int login()
{
x:check=false;
ifstream login("user.dat",ios::in|ios::binary);
if(!login) {
cout << "Cannot open file!" << endl;
return 1;
}
cout<<"Enter your first namen";
cin>>firstname;
cout<<"Enter your last name:"<<endl;
cin>>lastname;
cout<<"Enter passwordn";
cin>>password;
login.seekg(0);
for(int i = 0; i < userno ;i++)
{
login.read((char*)&u[i],sizeof(u[i]));
if(firstname == u[i].fname && lastname == u[i].lname &&
password == u[i].pword)
{
check=true;
cout<<"IT WORKS!!!!";
}
cout<<check;
}
if(check == false)
{
cout<<"Incorrect username or password!!!!n";
goto x;
system("cls");
}
login.close();
system("cls");
}
for 循环读取文件中的每个记录,但即使输入了正确的信息,仍会将检查输出为 false(0(。
您的问题缺少最小完整和可验证的示例(又名 mcve(,因此不可能说您的代码是否有更多问题,尽管一个问题是:
for(int i = 0; i < userno ;i++)
{
login.read((char*)&u[i],sizeof(u[i]));
if(firstname == u[i].fname && lastname == u[i].lname &&
password == u[i].pword)
{
check=true;
cout<<"IT WORKS!!!!";
}
cout<<check;
}
循环遍历u
元素,并在每次迭代中检查名称和密码是否匹配。循环后,check
只会告诉您最后一个条目是否匹配,但如果匹配是之前的,check
将在下一次迭代时被false
覆盖。
找到匹配项后,您需要打破循环:
for(int i = 0; i < userno ;i++)
{
login.read((char*)&u[i],sizeof(u[i]));
if(firstname == u[i].fname && lastname == u[i].lname &&
password == u[i].pword)
{
check=true;
cout<<"IT WORKS!!!!";
break; // exit the while loop
}
}
cout<<check; // move the print out of the loop
此外:
当- 您可以使用更清晰的
while
循环时,不要使用goto
- 阅读为什么使用命名空间 std 被认为是不好的做法
- 除非以后需要它,否则无需将文件中的所有条目存储在数组中。在循环中,您只需要一个条目。
相关文章:
- C++,系统无法执行指定的程序
- 在UNIX系统中使用DIR查找文件的字节大小
- 错误处理.将系统错误代码映射到泛型
- 当系统的卷被修改时,如何修改WASAPI环回捕获卷
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 在C++游戏中与库存系统作斗争
- 文件系统:复制功能的速度秘诀是什么
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 使用二进制文件的用户名和登录系统
- 登录系统C++:输入我的字符串User&Pass后,它要求我重新输入它们
- 获取 Windows 系统已加入 Azure AD 时登录用户的 UserPrincipalName
- 使用 c++ 的登录系统
- C 中的涡轮登录系统不起作用
- 在多个登录用户系统中检索当前登录的用户 SID
- ssh 使用系统(char * 命令)远程登录到服务器,并执行命令
- 如何使用system()在c中执行2个成功的系统命令,例如连接到ftp并使用程序登录
- 是否使用LDAP+kerberos作为身份验证程序启用SASL(带有GSSAPI)的客户端和服务器?(单点登录SSO系统
- 处理多个登录系统
- 用c++和MySQL实现一个登录系统
- 如果使用system用户令牌启动登录用户进程,是否会危及系统安全