c++ - 不从文件中读取数据

标签 c++

我做错了什么?我正在输入正确的凭据,但程序一直提示“用户名或密码无效!”

void user::login()
{
    string username, uname;
    string password, pword;

    cout << "Enter your username: \n"; 
    cin >> uname;
    cout << "Enter your password: \n"; 
    cin >> pword;

    ifstream data("data.txt");
    if (data.is_open())
    {
        while (!data.eof())
        {
            data >> username >> password;

            if (uname == username && pword == password)
            {
                cout << "Login successfully!\n";
                Sleep(2000);
                mainMenu();
            }
            else if (uname != username || pword != password)
            {
                cout << "Invalid username or password!\n";
                Sleep(2000);
                login();
            }
        }
        data.close();
    }
}

最佳答案

请看我的内联评论:

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

// You should not use using namespace std! In here only for brevity.
using namespace std;

// Use a proper API: return account name on success, empty string otherwise.
// Throw an exception on error.
string login() throw (const char*)
{
    // embrace C++11!
    if (ifstream data {"data.txt"}) {
        string username, uname;
        string password, pword;

        // Always test if the IO is broken!
        if (!((cout << "Enter your username: \n") &&
              (cin >> uname) &&
              (cout << "Enter your password: \n") &&
              (cin >> pword))) {
            throw "Could not get user credentials.";
        }

        // debug output, remove in production:
        cerr << "Credentials: " << uname << " (" << pword << ")" << endl;

        // read a line from data.txt
        while (data >> username >> password) {
            // debug output, remove in production:
            cerr << "Read: " << username << " (" << password << ")" << endl;

            // compare input:
            if ((uname == username) && (pword == password)) {
                return username;
            }
        }

        // no input matched
        return "";

        // no need to explicitly close the stream
    } else {
        throw "Password file could not be opened.";
    }
}

int main() {
    string account_name;
    try {
        // The caller should include a loop, not the login function.
        account_name = login();
    } catch (const char *message) {
        cerr << "Error: " << message << endl;
        return 1;
    }
    if (!account_name.empty()) {
        cout << "Hello, " << account_name << endl;
    } else {
        cout << "Could not authenticate!" << endl;
    }
    return 0;
}

数据.txt:

Neuroosi hunter2
Kay supersecret
Arunmu hello
crashmstr password1234

关于c++ - 不从文件中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34155748/

相关文章:

c++ - 任意长度字节组的弗莱彻校验和

c++ - 在没有实例的情况下调用无状态 lambda(仅类型)

c++ - 使用 boost::asio::ip::tcp::iostream 的低带宽性能

c++ - ';' 标记之前的预期主表达式错误

c++ - 如何测试实例是否损坏?

c++ - QT QPrintDialog打印机初始化错误如何解决?

c++ - UTF16 转换因 utfcpp 失败

c++ - 船上有数字的最短路径

c++ - if 语句中的结构化绑定(bind)初始化程序无法编译

c++ - 用于移动开发和机顶盒应用的嵌入式 Linux