从txt文件中读取用户名和密码
我需要从 .txt 文件中提取用户名和密码,但我很难思考到底如何做到这一点。我会尝试打破这个局面。
- 打开文件
- 读入用户名
- 将用户名与用户输入进行比较
- 将密码与与用户名关联的用户输入进行比较
- 如果用户名和密码匹配,则返回 true 或 false 是
的,这是家庭作业。我正在学习如何使用 fstream,同时等待 USPS 运送我的班级 txt 书。非常感谢您的帮助!
这是我到目前为止所得到的:
bool User::check(const string &uname, const string &pass)
{
//open the file
fstream line;
line.open("users.txt");
//Loop through usernames
//If a username matches, check if the password matches
}
users.txt 文件如下所示:
ali87 8422
ricq7 bjk1903
messi buneyinnessi
mike ini99ou
jenny Y00L11A09
end
I need to pull usernames and passwords from a .txt file and I am having a difficult time wrapping my mind around how exactly to do this. I'll try to break this down.
- Open the file
- Read in the Usernames
- Compare a username against a user input
- Compare a password against a user input associated with the username
- return true or false if the username and passwords match
Yes, this is homework. And I am learning how to use the fstream while waiting for USPS to ship my class txt book. Your help is greatly appreciated!
Here is what I have so far:
bool User::check(const string &uname, const string &pass)
{
//open the file
fstream line;
line.open("users.txt");
//Loop through usernames
//If a username matches, check if the password matches
}
The users.txt file looks like this:
ali87 8422
ricq7 bjk1903
messi buneyinnessi
mike ini99ou
jenny Y00L11A09
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为以下伪算法可能是您更好的选择:
对于步骤 3,您可以将每个行缓冲区存储在一个字符串中,您可以将其存储在字符串容器中。
理想情况下,在此处理过程中,您可以将字符串拆分为用户名、密码对,然后将它们存储在 std::map 中;然后通过map.find(输入用户名)==输入密码访问它。
您不需要将地图存储超过登录过程的持续时间,然后您应该丢弃该地图(可能作为本地函数变量)。
如果你的程序确实有一个目的,那么这是理想的,否则,就让它工作吧:)。
I think the following pseudo-algorithm may be a better option for you:
For step 3, you would store each line buffer in a string, which you could store in a container of strings.
Ideally, during this processing, you might split the string into a username, password pair, and then store them in a std::map; and then access this via map.find(input username) == input password.
You shouldn't need to store the map for more than the duration of the login process, you should then discard the map (maybe as a local function variable).
If your program actually has a purpose, this is ideal, otherwise, just get it working :).
我包括了 iostream、fstream 和 cstring。并使用
命名空间std
。users.txt
如下所示:用户名和密码之间有一个空格。(也没有项目符号)
I included
iostream
,fstream
andcstring
. And usingnamespace std
.users.txt
looks like this:there is one white space between username and password.(also without bullets)
试试这个:
http://www.learncpp.com/cpp-tutorial/136-基本文件io/
Try this:
http://www.learncpp.com/cpp-tutorial/136-basic-file-io/