如何在 c++ 中将密码和用户名保存到 .txt 文件中.如果用户尝试登录,我仍然希望能够检索它们

How do I save passwords and usernames to a .txt file in c++. I still want to be able to retrieve them if the user tries to login

本文关键字:用户 登录 希望 检索 文件 密码 c++ txt 保存 如果      更新时间:2023-10-16

我正在启动一个基于控制台的在线银行(为了好玩(。我想知道如何保存注册帐户的人的用户名和密码(也许是.txt文件?我也想知道当他们尝试登录时,我将如何检查.txt文件中的用户名和密码。任何帮助,不胜感激。这是源代码

#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <string>
#include <stdlib.h>
using namespace std;
bool gameOver;
// global variables
string EntryChoice;
int numberOfIncorrect = 5;
string NewUsername;
string NewPassword;
string LoginUsername;
string LoginPassword;
string NewFirstName;
string LoginFirstName;
string NewLastName;
string LoginLastName;
int Newage;
string Newgender;
int Loginage;
string LoginGender;
//declaration of functions
void Login();
void SignUp();
void BankCheck();
void BankError();
void Bank()
{
cout << "Welcome to the Bank of National Arabsn";
cout << "-------------------------------------n";
cout << "|To Sign Up type (Register) then Press Enter|n";
cout << "|To Login type (Login) then Press Enter|n";
cin >> EntryChoice;
BankCheck();
}
void BankCheck()
{
if (EntryChoice == "Login" || EntryChoice == "LOGIN" || EntryChoice == "login")
{
Login();
}
else if (EntryChoice == "REGISTER" || EntryChoice == "register" || EntryChoice == "Register")
{
SignUp();
}
else
{
system("cls");
BankError();
}
}
void BankError()
{
if (numberOfIncorrect == 1)
{
exit(1);
}
numberOfIncorrect -= 1;
cout << "Welcome to the Bank of National Arabsn";
cout << "-------------------------------------n";
cout << "|To Sign Up type (Register) then Press Enter|n";
cout << "|To Login type (Login) then Press Enter|n";
cout << "|ERROR| " << numberOfIncorrect << " Tries Left >> ";
cin >> EntryChoice;
BankCheck();
}
void Login()
{
system("cls");
cout << "Bank of United Arabs Login Pagen";
cout << "-------------------------------n";
cout << "Username: ";
cin >> LoginUsername;
cout << "nPassword:";
cin >> LoginPassword;
}
void SignUp()
{
system("cls");
cout << "Welcome to the Register Pagen";
cout << "----------------------------n";
cout << "Age: ";
cin >> Newage;
cout << "nGender: ";
cin >> Newgender;
cout << "nFirst name";
cin >> NewFirstName;
cout << "nLast Name";
cin >> NewLastName;
cout << "nUsername: ";
cin >> NewUsername;
cout << "nPassword";
cin >> NewPassword;
}
int main()
{
Bank();

return 0;
}

是的,你可以这样做。也许,每次您在注册时输入用户的数据时,都可以使用 c++ 文件运算符(阅读此内容:https://www.bogotobogo.com/cplusplus/fstream_input_output.php(同时将其存储到 txt 文件中,后跟一个分隔符来标记每条记录的末尾。 将文本文件中的格式保留为:

UserID1
Username1
Password1@
UserID2
Username2
Password2@

这里 @ 是一个分隔符。 要检索记录,请使用上述分隔符读取记录,在.txt文件中搜索用户名或 ID。如果找到记录,则按行分隔记录值并将其存储在变量中,然后与输入数据值进行比较。继续搜索,直到 EOF 标志为假。