从C ++中的文本文件依次检查用户名和密码

兰迪梅尔斯

fstream没用太多,所以我有点迷路了。我创建了一个文本文件,其中包含一个随机单词列表,我希望将其用作程序的用户名和密码列表。

我希望程序检查用户是否存在(该行中的第一个字符串),然后检查其后的第二个单词是否“匹配”。

到目前为止,我有这个:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream inFile;
    inFile.open("userData.txt");

    // Check for error
    if (inFile.fail()) {
        cerr << "error opening file" << endl;
        exit(1);
    }

    string user, pass;
    int Count = 0;

    // Read file till you reach the end and check for matchs
    while (!inFile.eof()) {
        inFile >> user >> pass;
        if (user == "Banana", "Apple") {
            Count++;
        }
        cout << Count << " users found!" << endl;
    }
}

我的文本文件包含:

香蕉苹果/ n
胡萝卜草莓/ n
巧克力蛋糕/ n
奶酪派/ n

我发现我的代码现在不好,但是我真的不知道我在做什么。

不存在

参见下文:

while (!inFile.eof()) {
    inFile >> user >> pass;
    if (user == "Banana", "Apple") {
        Count++; // No point in doing so because this only happens once
    }
    cout << Count << " users found!" << endl;
}

使用while (inFile >> user >> pass){代替while (!inFile.eof()){为什么?

尝试以下方法:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream inFile;
    inFile.open("userData.txt");

    // Check for error
    if (inFile.fail()) {
        cerr << "error opening file" << endl;
        exit(1);
    }

    string user, pass;
    int Count = 0;

    // Read file till you reach the end and check for matchs
    while (inFile >> user >> pass) {
        if (user == "Banana" && pass == "Apple") {
            cout <<"user found!" << endl;
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

检查文本文件中的用户名和密码

如何检查文本文件中的用户名和密码

在 Python 中验证文本文件中的用户名和密码

从文本文件中读取用户名和密码的程序

没有从文本文件中获取用户名和密码

检查文本文件在C ++中的格式是否正确

C程序未读取用户名和密码的.txt文件

c#VssBasicCredential与用户名和密码

如何在C#中验证Windows应用程序的咸密码和用户名?

如何将用户名密码和密码与文本文件进行比较以登录java

通过https从C#以纯文本格式发送用户名和密码到Wordpress网站是否安全?

如何在控制台中创建登录名,在其中使用存储在文件 c# 中的用户名和密码进行身份验证

如何使用C#向包含用户输入的文本文件写入和读取列表

从文本文件中提取用户名

从文本文件创建用户名

C#检查文本文件中的EOF以修复数组

如何使用 C# 检查文本文件中是否存在名词?

如果文本文件中的用户名相同,则替换文本行吗?

将用户输入保存到文本文件C ++

从文本文件获取登录日期时间和用户名的计数

使用PowerShell从文本文件中提取用户名和电子邮件

在c#中分别读取文本文件和访问文本文件的元素

用php覆盖包含特定用户名的文本文件中的一行

如何在一个文本文件中存储多个用户名

我如何从文本文件中输入文本 (c)

我想从 C# 中的文本文件中搜索、删除和更新

AFNetworking对用户名和密码参数的异步POST请求(Objective-C iOS)

C / HTML-从HTML表单打印用户名和密码

如何使用目标 C 使用 textFieldDidChange 验证用户名和密码?