Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Usernames and Passwords from a txt file

I need to pull usernames and passwords from a .txt file and I am having a difficult time wrapping my mind around how exactly to do this. I'll try to break this down.

  1. Open the file
  2. Read in the Usernames
  3. Compare a username against a user input
  4. Compare a password against a user input associated with the username
  5. return true or false if the username and passwords match

Yes, this is homework. And I am learning how to use the fstream while waiting for USPS to ship my class txt book. Your help is greatly appreciated!

Here is what I have so far:

bool User::check(const string &uname, const string &pass)
{
    //open the file

    fstream line;
    line.open("users.txt");

    //Loop through usernames
        //If a username matches, check if the password matches
}

The users.txt file looks like this:

ali87   8422

ricq7   bjk1903

messi   buneyinnessi

mike    ini99ou

jenny   Y00L11A09

end
like image 615
Jimmathy Avatar asked Oct 19 '25 14:10

Jimmathy


1 Answers

I think the following pseudo-algorithm may be a better option for you:

  1. Input username, password
  2. Open file stream to file
  3. Search stream for username match (exit if non found)
  4. If found, compare encrypted input password against stored encrypted password.
  5. If found, return success, else, "No username found or password incorrect.".

For step 3, you would store each line buffer in a string, which you could store in a container of strings. Ideally, during this processing, you might split the string into a username, password pair, and then store them in a std::map; and then access this via map.find(input username) == input password.

You shouldn't need to store the map for more than the duration of the login process, you should then discard the map (maybe as a local function variable).

If your program actually has a purpose, this is ideal, otherwise, just get it working :).