4

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
hiddensunset4
  • 5,825
  • 3
  • 39
  • 61
Jimmathy
  • 327
  • 8
  • 23
  • Mike, I would like to ask you what you think the "high level" goal of this project is? It looks like it's a little more then just understanding fstream...am I correct? (Teachers really like to pull that, putting "extra learning" in every assignment, who do they think they are!) – onaclov2000 Apr 23 '11 at 03:11

3 Answers3

2

I included iostream, fstream and cstring. And using namespace std.

int main()
{
char login_password[20];
char stored_password[20];   
char login_username[20];
char stored_username[20];

fstream pull("users.txt",ios::in);
if (!pull) { 
    cout<<"File not loaded!"<<endl;
    return -1; 
}
cout<<"Username: ";
cin>>login_username;
while(strcmp(login_username,stored_username)){ 

//if login and stored usernames are equal, function strcmp returns 0,
//at first loop they are certainly not, so it is: while(1)

    pull>>stored_username;
    if(pull.eof()){   //if it is the end of file
        cout<<"Username does not exist. "<<endl;
        return -1;  
    }
}
pull>>stored_password; 

//since username and password are in the same line, password next to
//correctly inputted username is saved in stored_password

cout<<"Password: ";   
//now user enters password to confirm username
cin>>login_password;
while(strcmp(stored_password,login_password)){
    cout<<"Wrong password. "<<endl;
    cout<<"Try again: ";
    cin>>login_password;
}
cout<<"Login successful."<<endl;
return 0;
}

users.txt looks like this:

  • Lena84 uzumymw
  • Doris20 kjkszpj

there is one white space between username and password.(also without bullets)

zx485
  • 28,498
  • 28
  • 50
  • 59
Adriana
  • 141
  • 1
  • 3
2

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 :).

hiddensunset4
  • 5,825
  • 3
  • 39
  • 61
  • I don't know for sure but if the person's learning fstream, I am going to assume that they're pretty new to either programming, or this language, BUT I will say that the first thing I thought of when I read the question was that it was much like a hash table. I like your answer, although it might be more advanced then the Mike knows how to do, but it would be good practice. I upvoted ya! – onaclov2000 Apr 23 '11 at 03:09
1

Try this: http://www.learncpp.com/cpp-tutorial/136-basic-file-io/

Tim
  • 834
  • 2
  • 12
  • 31