Reading any line of a file

Hello folks, i'm a newbie herein this forum.

I'm using QT Creator in compiling my work.

i can't seem to read the next line of my file, whenever i switch username, password and fullname:

1
2
3
username;password;fullname
user1;1234;reese reese's
user2;123456;snicky snickers 


here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    std::ifstream inFile("/signup.txt");
    char str[255];
    

        std::string uname;
        std::string pword;
        QString username = ui->lineEditUsername->text();
        inFile.getline(str,255,';');
        uname = str;
        QString password = ui->lineEditPassword->text();
        inFile.getline(str,255,';');
        pword = str;

        if(uname == username.toStdString() && pword == password.toStdString())
        {
                std::string fullname;
                std::string line;
                inFile.getline(str,255,'\n');
                fullname.append("Welcome ");
                fullname.append(str);
                ui->label->setText(fullname.c_str());

                while(std::getline(inFile, line))
                {
                    line->push_back(line);
                }

        } else
            {
                ui->label->setText("Invalid login.");
            }

    inFile.close();


Is there something that i should do to make my code work?
Last edited on
Better use the std::string version of getline() (line 8/11):

http://www.cplusplus.com/reference/string/string/getline/?kw=getline

like you do on line 23. The first version has a delimiter as well.

What is the first line? The title? If so you need to either ignore the first line or use it to determine whether the input file is correct.

Line 14 is supposed to be a loop. You need to check each entry in the file for a match:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool is_match = false;
while(std::getline(inFile, uname, ';') && std::getline(inFile, uname, ';') && std::getline(inFile, fullname))
{
        if(uname == username.toStdString() && pword == password.toStdString())
        {
                is_match = true;
                break;
        }
}
        if(is_match)
        {
                ui->label->setText(("Welcome " + fullname).c_str());

        } else
            {
                ui->label->setText("Invalid login.");
            }
I already tried your suggestion on line 8 and 11 to use the std::string version of getline it only says:

 
'std::getline' is not a class member


=====================

1
2
3
username;password;fullname
user1;1234;reese reese's
user2;123456;snicky snickers 


and without altering line 8 and 11, your suggested while loop only reads the username and password correctly, meanwhile, the 3rd part (names) it skips to the last line and reads only snicky snickers

"fullname"and "reese reese's" can't be read. Anyway, thanks. (my code reads any line of the file now.)

EDIT: I just found out that setting while to check if username and password matches, it assumes all line are true even if either username and password is blank.
Last edited on
I already tried your suggestion on line 8 and 11 to use the std::string version of getline it only says:

Show your code where you're using the std::string then perhaps someone can advise you as to what you're doing wrong.

Did you #include the <string> header file?
I did include the <string> header file.

here's the code so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void MainWindow::on_pushButtonLogin_clicked()
{
    //checks for a match in each entry on the same line
    bool isMatch = false;

    std::ifstream inFile("/signup.txt");
    char str[255];

    std::string uname;
    std::string pword;
    std::string fullname;
    QString username = ui->lineEditUsername->text();
    inFile.std::getline(str,255,';');
    uname = str;
    QString password = ui->lineEditPassword->text();
    inFile.std::getline(str,255,';');
    pword = str;

    while(std::getline(inFile, uname, ';') && std::getline(inFile, uname, ';') && std::getline(inFile, fullname))
    {
        if(uname == username.toStdString() && pword == password.toStdString())
        {
            //this->hide(); //OPTIONAL::this will hide the login window
                              //won't show welcome <fullname> label.

            isMatch = true;
            break;
        }
    }
    if (isMatch)
    {
        //outputs the correct name of the user.
        ui->label->setText(("Welcome " + fullname).c_str());

        //will open a new window after successfull login
        PeriodicTable elements;
        elements.setModal(true);
        elements.exec();

    } else
    {
        ui->label->setText("Invalid login.");
    }
    inFile.close();
}


line 13 and 16 says it is not a class member, even if <string> header file is included.
Last edited on
Line 19: Change the second uname -> pword

while(std::getline(inFile, uname, ';') && std::getline(inFile, pword, ';') && std::getline(inFile, fullname))

Line 19: Change the second uname -> pword

Line 13: std::getline(inFile, str,';');
Line 16: std::getline(inFile, str,';');

They don't even make sense (remove line 13/14/16/17). Instead read the full line before the while loop:

1
2
3
    std::string str;
    std::getline(inFile, str);
    while(std::getline(inFile, uname, ';') && std::getline(inFile, pword, ';') && std::getline(inFile, fullname))



Thank you sir coder777 for that.

Now isMatch = true inside the if statement doesn't work, it always output an invalid login in every user.

http://i.imgur.com/eVWaPAM.png
Last edited on
I checked it with the changes and it works as expected.

Did you check whether the file could be opened?
Last edited on
now i'm confused

http://i.imgur.com/4eSdj29.png


EDIT:

i put some minor changes, noticed that i removed char and changed to string

but still if(isMatch) doesn't work properly, it will jump to invalid login instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void MainWindow::on_pushButtonLogin_clicked()
{
    //checks for a match in each entry on the same line
    bool isMatch = false;


    std::ifstream inFile("D:/signup.txt");
    //char str[255];

    std::string str;
    std::getline(inFile, str);

    std::string uname;
    std::string pword;
    std::string fullname;
    QString username = ui->lineEditUsername->text();
    uname = str;
    QString password = ui->lineEditPassword->text();
    pword = str;

    while(std::getline(inFile, uname, ';') && std::getline(inFile, pword, ';') && std::getline(inFile, fullname))
    {
        if(uname == username.toStdString() && pword == password.toStdString())
        {
            //this->hide(); //OPTIONAL::this will hide the login window
                              //won't show welcome <fullname> label.

            isMatch = true;
            break;
        }
    }
    if (isMatch)
    {
        //outputs the correct name of the user.
        ui->label->setText(("Welcome " + fullname).c_str());

        //will open a new window after successfull login
        PeriodicTable elements;
        elements.setModal(true);
        elements.exec();
    } else
    {
        ui->label->setText("Invalid login.");
    }
    inFile.close();
}


yes, the file can be open, i put

1
2
3
4
5
if (inFile.is_open())
             {
                std::cout << "File was created successfully!" << endl;
             }
                else std::cout << "Unable to open your file.";


and the compiler issued, std::cout << "File was created successfully!" << endl; will always evaluate to true, so i guess it is accessible right?
Last edited on
I did not test it with the qt stuff (just with console). You need to debug in order to figure out where it is going wrong.
Thank you sir.
Topic archived. No new replies allowed.