测试.txt文件中的重复用户名

Testing for duplicate usernames from a .txt file

本文关键字:复用 用户 txt 文件 测试      更新时间:2023-10-16

我想尝试编写一个登录/注册程序。使用一个外部.txt文件作为我的数据库,我想在其中保留"注册"的用户和密码。问题:我真的无法测试用户名的重复

// Currently stuck here
//Issue is that the if statment is not testing the way i want it to (comparing the usernames)
//The .txt file that i am using contains the content: "Usernames: "
//getline(myfile,line) returns "Usernames:" which is not really finding if there are any duplicates of usernames

ifstream myfile(filename.c_str());
while(getline(myfile,line)){
cout<< line;
if(line.find("Username: " + userregtry) == string::npos){
myfile.close();
ofstream writein(filename.c_str(),ios::app);
writein<< "Username: " << userregtry << "n" ;
}
else{
cout<< "nThis username has been takennPlease try another username: ";
}
}

假设每行有一个名称

有几个问题:

  • 如果名称不在第一行,则认为名称不存在,并关闭文件,停止搜索

  • 如果名字出现在第一行,你会发出警告,然后继续,如果第二行没有名字,你会认为这个名字不存在

  • 如果文件为空,它将保持为空

在考虑名称是否缺失之前,您需要查看所有行。

解决方案:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string filename = "/tmp/foo";
string userregtry;
cout << "nPlease enter username: ";
for (;;) {
if (!(cin >> userregtry))
break;
ifstream myfile(filename.c_str());
bool found = false;
string line;
while (getline(myfile,line)) {
if (line.find("Username: " + userregtry) != string::npos) {
found = true;
break;
}
}
myfile.close();
if (found)
cout<< "nThis username has been takennPlease try another username: ";
else {
ofstream writein(filename.c_str(),ios::app);
writein<< "Username: " << userregtry << "n" ;
break;
}
}
return 0;
}

从空文件执行:

/tmp % ./a.out
Please enter username: aze
/tmp % ./a.out
Please enter username: aze
This username has been taken
Please try another username: qsd
/tmp % ./a.out
Please enter username: qsd
This username has been taken
Please try another username: aze
This username has been taken
Please try another username: iop
/tmp % cat foo
Username: aze
Username: qsd
Username: iop

假设文件包含一行Usernames: name1 name2 ... namen

有几个问题:

  • 您的代码找不到名称,除非它位于的第一个位置

  • 当你添加一个新名称时,你会失去所有其他

  • 用户名或用户名s

唯一线路未终止的解决方案:

#include <fstream>
#include <string>
using namespace std;
int main()
{
string filename = "/tmp/foo";
string line;
{
ifstream myfile(filename.c_str());
if (getline(myfile,line))
line += ' '; // to have a space before and after each name including the last
}
string userregtry;
cout << "Please enter username: ";
for (;;) {
if (!(cin >> userregtry))
return 0;
if (line.find(' ' + userregtry + ' ') != string::npos)
cout << "This username has been takennPlease try another username: ";
else {
ofstream writein(filename.c_str(),ios::app);
if (line.empty())
writein << "Usernames: ";
else
writein << ' ';
writein << userregtry;
return 0;
}
}
}

从空文件执行:

Please enter username: aze
/tmp % ./a.out
Please enter username: aze
This username has been taken
Please try another username: qsd
/tmp % ./a.out
Please enter username: aze
This username has been taken
Please try another username: qsd
This username has been taken
Please try another username: wxc
/tmp % cat foo ; echo "#"
Usernames: aze qsd wxc#