测试.txt文件中的重复用户名
Testing for duplicate usernames from a .txt file
我想尝试编写一个登录/注册程序。使用一个外部.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#
相关文章:
- 使用 avcodec_decode_subtitle2 (FFmpeg) 提取(解复用)字幕
- C++用用户输入在循环中填充 char 数组:输入整个字符串时到底发生了什么?
- 使用 Select 多路复用未命名的管道和其他文件描述符
- FFMPEG:具有不同持续时间的多路复用流
- 不推荐使用 AVStream.codec.time_base 作为复用器的时基提示.改为设置AVStream.time_
- 如何将文件复制到另一个文件中,但是用用户输入的单词替换单词
- 如何加密用用户输入的文件写的消息
- 输入复用的c++设计模式
- 用用户输入C 动态分配的数组
- 搜索具有多路复用的C/C 网络库
- C++IO/多路复用TCP服务器和POSIX线程
- C++I/O多路复用服务器过早关闭连接
- 用c++打印的多路复用值错误
- 用用户实现的交换来交换std::函数
- VLC libx264流复用为FLV
- 如果cv::Mat是用用户分配的数据构建的,那么它会负责删除它吗
- 使用select()、read()、write()进行I/O多路复用
- 每个会话连接或通过一个连接复用多个会话
- 如何使用boost::statechart在固定数量的线程上复用许多异步状态机
- 用用户输入打开不同目录下的文件