Text File Authentication in C ++

I have an auth.txt file containing a username and password. my goal is to use this file to find out if the user enters a valid username and password before moving on to the next menu. eg. auth.txt contains the user \ n pass. when they select the menu, he asks them to enter the system. if they enter incorrectly, he does nothing. each password and usrname are stored in the auth.txt file. I tried using the following code but received nothing. please help and in advance.

if(getline(inauth, line)){ 

    if(line==user&& line==password){ 
    //your in

    }else cout<<"bye";
    }
+3
source share
3 answers

, "", "". , , . getline . , . - :

ifstream inauth("Secret password herein.txt");

if (inauth) {
    string usr, psw;

    if (getline(inauth, usr) && getline(inauth, psw) {
        if (usr == user && psw == password) {
            cout << "Phew, everything fine.";
        } else {
            cout << "Wrong password/username.";
        }
    } else {
        cout << "I guess somebody opened the file in notepad and messed it up."
    }
} else {
    cout << "Can't open file, sorry.";
}
0

V++, , .

// keep looping while we read lines
while (getline(inauth, line)) 
{
    // valid user
    if (line == user) 
    {
        // read the next line
        if (getline(inauth, line2))
        {
            if (line2 == password)
            {
                // successfully authenticated
                cout << "Success!";
            } 
            else 
            {
                // valid user, invalid password
                // break out of the while loop
                break;
            }
        }
    }
}
+1

if your username and password are stored on the same line, marked with a space, say, a space, you will need

#include <sstream>

string line, username, password;
istringstream instream;
while (getline(inauth, line))
{
    // extract username and password from line using stringstream
    instream.clear();
    instream.str(line);
    instream >> username >> password;
    // do something here
}
0
source

All Articles