簡體   English   中英

文件中的用戶名和密碼

[英]Username and password within files

這可能已經在某個地方得到了回答,但是我花了幾分鍾尋找,卻找不到任何東西。 所以我有兩個文件, User.txtPass.txt

Pass.txt看起來像這樣:

password
test

User.txt看起來像這樣:

username
test

這是我的代碼在這里:

        cout << "Username: " << endl; //input user
        cin >> user;
        cout << "Password: " << endl; //input pass
        cin >> pass;
        userfile.open("User.txt");
        passfile.open("Pass.txt");
        while (getline(userfile, STRINGT) && getline(passfile, STRINGO))
        {
            if (user == STRINGT.c_str() && pass == STRINGO.c_str()) //if user and pass are both in file
            {
                //i had a break here, (its in a switch) so i replaced it with a cout, but it didn't change
            }
        }
        cout << "Incorrect username or password" << endl;
        _getch(); //pause
        return 0; //end program

每當我跑步時,都會說每次都不正確。 我正在檢查所有大寫字母,什么都找不到,無法解決問題。

我已經在上面聲明了所有其他必要的fstream文件

在此先感謝C:

這是您需要的:

    bool found = false;

    while (getline(userfile, STRINGT) && getline(passfile, STRINGO))
    {
        if (strcmp(user, STRINGT.c_str()) == 0 && strcmp(pass, STRINGO.c_str()) == 0) //if user and pass are both in file
        {
            found = true;
            break;
        }
    }

    if (!found) {
        cout << "Incorrect username or password" << endl;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM