I am working on a project for an assignment and I need some help with QFile and QTextStream. I want to initially create a File (named: users.txt) that has this inside of it:
ID Password Name and Surname Username
1 | cheese | Daniel Markov | MaDanjel
2 | something | Mika Firring | Marila
..
..
So far I get the following contents of skrivno.txt
:
0202|Nejc Zun|nejczun|
This is the code:
void registracija::write() {
QFile username("skrivno.txt");
if(!username.open(QIODevice::Append))
QMessageBox::information(0,"Error",username.errorString());
QTextStream u1(&username);
username.open(QIODevice::Append | QFile::Text);
u1 << ui->lineEdit_3->text()+"|"; //code
u1 << ui->lineEdit_4->text()+"|"; //name and surname
u1 << ui->lineEdit_5->text()+'\n'; //password
username.close();
}
My plan was to than call the text from this function:
void MainWindow::on_pushButton_3_clicked()//login
{
QFile username("skrivno.txt");
if(!username.open(QIODevice::ReadOnly))
QMessageBox::information(0,"Error",username.errorString());
QTextStream u1(&username);
username.open(QIODevice::ReadOnly | QFile::Text);
QString temp;
while (!u1.atEnd()) {
temp = u1.readLine();
if((ui->lineEdit_2->text()== temp.section("|",0,0)) && (ui->lineEdit->text()== temp.section("|",2,2))){
ui->label_2->setText("Welcome: " + temp.section("|",1,1));
ui->progressBar->show();
for(int a=0;a<101;a++){
ui->progressBar->setValue(a);
}
Open_Window();
ui->progressBar->close();
ui->un1->close();
ui->un2->close();
ui->label_3->close();
player->stop();
}else{
ui->label->close();
ui->un1->show();
ui->un2->show();
ui->label_3->show();
player->stop();
}
}
username.close();
}
And that it could be able to read from different users so it checks the users Username and Password, if it's the correct user with the correct password it let's him open the things below.
This is the code that tests only the 1st line of the File skrivno.txt
if((ui->lineEdit_2->text()== temp.section("|",0,0)) && (ui->lineEdit->text()== temp.section("|",2,2)))
And now I need help with this so not only does it write the ID in the text, but also checks all the users and their passwords if they are the correct ones, not only the first one in the file.
My code works. I need help improving the code by:
- adding the ID in the text file (I dont know how to add it by 1 every time someone registers a new user.
- checking every user and their password (not only the first one)
Something like this:
void registracija::write() {
QFile username("skrivno.txt");
if(!username.open(QIODevice::Append))
QMessageBox::information(0,"Error",username.errorString());
QTextStream u1(&username);
username.open(QIODevice::Append | QFile::Text);
u1 << SOMEHOW ADD THE ID IN HERE AND ALWAYS ADD IT +1 // <= help needed
u1 << ui->lineEdit_3->text()+"|"; //code
u1 << ui->lineEdit_4->text()+"|"; //name and surname
u1 << ui->lineEdit_5->text()+'\n'; //password
username.close();
}