0

I am making a user database. When I try to open the "dataBase.txt" which holds all of the users and passwords, the console pops up (which should happen since it is a console application) but it says that the program has already complete. When I close it, my computer tell me that the program had crashed. The function is saved in a class.

After some debugging the code seems to crash at ifstream fin("dataBase.txt");

No error is returned from the compiler.

The code for the function called is:

void User_Psw::UserCheck()
{
    // read from the database
    ifstream fin("dataBase.txt");

    while (!fin.eof())
    {
        fin >> Usernames[sizeOfDatabase] >> Password[sizeOfDatabase];
        sizeOfDatabase++; //The Number of lines in .txt
    }

    // rest of the program
    cout << "Username: ";
    cin >> username;

    getNameIndex();

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

    if(!PasswordMatches())
    {
        cout << "Access denied";
    }
}

I can add more code snippets if asked.

ponger3d
  • 27
  • 1
  • 6
  • 1
    I think you have more errors than that. Look at `fin`. If all else fails, read the documentation! – Tyler Jandreau Feb 10 '13 at 03:42
  • 1
    How are `Usernames` and `Passwords` declared? Also `while (!fin.eof())` is probably not doing what you think it's doing. – johnsyweb Feb 10 '13 at 03:42
  • 1
    Why do things like `Usernames[sizeOfDatabase]` make me think somewhere out there is a vector/deque/array/fixed-array that is about to have the memory one element past its allotted size get stomped? – WhozCraig Feb 10 '13 at 03:45
  • Well I call `Usernames` as `std::vector Usernames;` and `Password` as `std::vector Password;` And I think that `while (!fin.eof())` is checking the number of lines in the file and then saving it to the variable `sizeOfDatabase` which I can then use as an index. – ponger3d Feb 10 '13 at 03:48

2 Answers2

1

Don't use fin.eof() to control a loop. That function is only useful after a read has failed.

A likely reason for the crash, however, is that you are assigning to Usernames[sizeOfDatabase], which may be beyond Usernames.capacity(). The canonical way to append and item to a std::vector is to call push_back().

Since your containers are std::vector<std::string>, this is a better approach...

std::string username, password;
while (fin >> username >> password)
{
    Usernames.push_back(username);
    Passwords.push_back(password);
    ++sizeOfDatabase;
}

Of course, if you want to know the number of usernames or passwords once the file has been read, you can call Usernames.size() (which should be the same as Passwords.size()); this may obviate the need to keep sizeOfDatabase.

Personally, I would use a single container for usernames and (salted, hashed) passwords, rather than two separate containers; either std::map<std::string, std::string> or perhaps std::unordered_map<std::string, std::string>, making the password lookup nice and quick per username.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • Thank You!! that was the fix. So this was a better approach because it would just keep on adding a new new variables to the vars `username` and `password`? – ponger3d Feb 10 '13 at 03:56
  • Correct, it also stops you reading off the end of the file. Now, ***please*** tell me you are not storing passwords in plain text. – johnsyweb Feb 10 '13 at 03:57
  • Trust me, this is not for some huge project. I am doing this as a learning experience. They are stored in .txt right now, and later on I hope on learning how to encrypt them, perhaps with the function `crypt` if I know it right. Right now I am just trying to make it where you can have an account with your high score saved on it. – ponger3d Feb 10 '13 at 04:07
0

I think you should start by adding this to the constructor of your ifstream as you haven't specified you want the file opened for input:

ifstream fin( "dataBase.txt",  ifstream::in );

if( !fin.good() )
{
    cout << "Failed to open database file." << endl;
    return;
}

See http://www.cplusplus.com/reference/fstream/ifstream/ifstream/ for additional literature.

Matthew
  • 2,759
  • 18
  • 28
  • I made the `ifstrea::in` change and then put in the check to see if the file was good or not, but when I run the program no line pops up saying `Failed to open database file`. So I assume it is good but the program still crashes. – ponger3d Feb 10 '13 at 03:51
  • @ponger3d if `fin.good()` fails and you get the error message then perhaps you don't have access to the directory? I would first solve the issue causing the access to the file. – Matthew Feb 11 '13 at 04:19