简体   繁体   中英

C++: Reading from file /UserName and Password

I am doing a program that will prompt a user to login in using their username and password. I have a textfile(userandpassword.txt) that contains a list of username and password. so what happens is, the user input their username and password, and their input will be matched with the username and password in the textfile. if it matches, they can login and if it doesn't match, it will print out "Invalid username or password". below is what I have.

textfile(userandpassword.txt)

john abcd
mary efgh
jane ijkl

my codes

string line = " ";
ifstream readFile("userandpassword.txt");
string UserName;
string Password;
string _UserName;
string _Password;

cout << "Enter UserName: ";
cin >> UserName;

cout << "Enter Password: ";
cin >> Password;

while (getline(readFile,line)) {

    stringstream iss(line);
    iss >> _UserName >> _Password;

    if (UserName == _UserName && Password == _Password) {
        cout << "Login Successfully!"<< endl;
    }

    else {
        cout << "InValid UserName And Password"<< endl;
    }

}

The problem is if I key in username: john and password: abcd it will return me the output as shown below

Login Successfully!
InValid UserName And Password
InValid UserName And Password 

whereas if I key in username: mary and password: efgh it will return me the out as shown below

InValid UserName And Password
Login Successfully!
InValid UserName And Password

and finally username: jane and password: ijkl

InValid UserName And Password
InValid UserName And Password
Login Successfully!

but this is not the result I wanted, what I wanted is it reads through the file line by line for the username and password and if matches with the user's input , It will just output "Login Successfully" and same goes for if it does not match , it will just print out "Invalid Username and password".

eg

if I key in username: mary and password: efgh, my expected output

Login Successfully!

and if I key in any invalid username and password , my expected output

Invalid Username and password.

Any ideas or suggestions on how should I achieve my expected output? Thanks

Add a break after you found a correct line and output the invalid-message behind the while loop:

bool found = false;
while (getline(readFile,line)) {

    stringstream iss(line);
    iss >> _UserName >> _Password;

    if (UserName == _UserName && Password == _Password) {
        cout << "Login Successfully!"<< endl;
        found = true;
        break;
    }
}

if (!found) {
    cout << "InValid UserName And Password"<< endl;
}

Edit: By the way. It is not such a good idea to safe any password credentials in plain text files. You should safe a hash-value (salted) of the password, hash the user input, too and compare the hashes. This way, even if someone gets the file, he will have to break the hashing algorithm to find the actual password.

Edit2: A hash can be generated by one of the well-testes hash-functions available (sha-1, sha-3 eg). This choice depends on how secure it has to be and on the avaiability of code to calculate such a hash. The general process would be (pseudo-code):

cin >> username;
for-each-row in file do:
    read username-in, salt, password-hash-in
    if (username-in == username && hash(salt, password) == password-hash-in):
        success!

The salt is a small piece of data that is appended or somehow integrated into the password and is randomly generated while the password is stored to the file. This way, the hashes will be different - even if two users use the same password. The input-password has to be hashed with the same salt, this is why the salt has to be read from the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM