[Solved] C++ fstream – Account login/registration via txt file


If checking the username fails you still have extra data on that line that hasn’t been read in yet. Consider if the input data was the following

username1;password
username2;password

When you read the first line and the username does not match you have password dangling on the current input line. So now your input looks like this

password
username2;password

The next time you read in the username you get password username where the space is a non-printable line ending. This will continue until you read the entire file so only the first username and password pair in the file can ever be found.

To fix this you can use std::getline to skip the remaining text on the input line.

if(tempUser == username)
{
    // valid username. Read the password
}
else
{ 
    // skip rest of the line
    getline(openFile, tempUser);
}

Or read both values at the same time and simplify your logic

getline(openFile, tempUser, ';');
getline(openFile, tempPassword);

if(tempUser == username)
{
}

3

solved C++ fstream – Account login/registration via txt file