0

I am creating a somewhat weak/vague database (My experience is very little, and please forgive the mess of my code). For this, I create a check everytime my console program starts that checks whether a database (copied to userlist.txt) is created already, if not a new will be created, if the database exists, however, it should all be copied to a 'vector users' (Which is a struct) I have within the class 'userbase' that will then contain all user information.

My userstats struct looks like this,

enum securityLevel {user, moderator, admin};
struct userstats
{
    string ID;

    string name;
    string password;
    securityLevel secLev;                         
};

I will contain all this information from a textfile in this code,

    int main()
    {
        Userbase userbase;  // Class to contain userinformation during runtime.

        ifstream inFile;
        inFile.open("userlist.txt");


        if(inFile.good())
        {
            // ADD DATE OF MODIFICATION
            cout << "USERLIST FOUND, READING USERS.\n";
            userstats tempBuffer;
            int userCount = -1;
            int overCount = 0;

            while(!inFile.eof())
            {
                string buffer;

                getline(inFile, buffer);
                if (buffer == "ID:")
                {
                    userCount++;

                    if (userCount > overCount)
                    {
                        userbase.users.push_back(tempBuffer);
                        overCount++;

                    }
                    tempBuffer.ID = buffer;
                    cout << "ID";           // Just to see if works

                }

                else if (buffer == "name:")
                {
                    cout << "name";         // Just to see if works
                    tempBuffer.name = buffer;

                }
                else if (buffer == "password:")
                {
                    cout << "password";     // Just to see if works
                    tempBuffer.password = buffer;
                }



            }
            if (userCount == 0)
            {
                userbase.users.push_back(tempBuffer);
            }

            inFile.close();
        }
...

What I try to do is to read and analyze every line of the text file. An example of the userlist.txt could be,

created: Sun Apr 15 22:19:44 2012

mod_date: Sun Apr 15 22:19:44 2012


ID:1d
name:admin
password:Admin1
security level:2

(I am aware I do not read "security level" into the program yet)

EDIT: There could also be more users simply following the "security level:x"-line of the preceding user in the list.

Now, if the program reads the line "ID:1d" it should then copy this into the struct and finally I will put it all into the vector userbase.users[i]. This does not seem to work, however. It does not seem to catch on to any of the if-statements. I've gotten this sort of program to work before, so I am very confused what I am doing wrong. I could really use some help with this. Any other kind of criticism of the code is very welcome.

Regards, Mikkel

Boooke
  • 5
  • 1
  • 3

1 Answers1

1

None of the if (buffer == ...) will ever be true as the lines always contain the value of the attribute contained on each line as well as the type of the attribute. For example:

ID:1d

when getline() reads this buffer will contain ID:1d so:

if (buffer == "ID:")

will be false. Use string.find() instead:

if (0 == buffer.find("ID:")) // Comparing to zero ensures that the line
{                            // starts with "ID:".
    // Avoid including the attribute type
    // in the value.
    tempBuffer.ID.assign(buffer.begin() + 3, buffer.end());
}

As commented by jrok, the while for reading the file is incorrect as no check is made immediately after getline(). Change to:

string buffer;
while(getline(inFile, buffer))
{
    ...
Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Also, `while(!inFile.eof())` is likely to break. See here http://stackoverflow.com/questions/21647/reading-from-text-file-until-eof-repeats-last-line – jrok Apr 15 '12 at 22:32
  • Thank you very much for this. the '0 ==' is very useful and something I was not sure how to implement before. And thanks for the heads up concerning .eof(). I'll implement that aswell! – Boooke Apr 15 '12 at 22:37