Editing a certain number of lines in a QFile
-
I am building a patient system for a university project and I want to add an "Edit Information" feature. So after the patient logs in and the system verifies the username and password stored in a file, they are greeted with a welcome screen and one of those tabs allows them to edit information.
The patient data in the text files is as follows, please note that the "<" and ">" characters are used to identify when a new patients information is being stored (file is appended)< Markg MARK Mark Mark AUC 19 M A+ 0102 MARKgg 0102 >
The first line after the "<" is the username and the second one is the password, now I need to allow the patient to edit all of their information. How can I do so?
The code I used to allow the user to register is,
QFile file("filename.txt"); if(!file.open(QFile::WriteOnly | QFile::Text| QIODevice::Append)) { QMessageBox::warning(this, tr("ERROR"), tr("File is missing, please check the path!"), QMessageBox::Close); } QTextStream write(&file); QString Username=ui->username->text(); QString Password=ui->pass->text(); QString Name=ui->name->text(); QString Email=ui->email->text(); QString Address=ui->addr->text(); QString Age=ui->age->text(); QString Gender=ui->gen->currentText(); QString Bloodtype=ui->bt->currentText(); QString Phone=ui->pnum->text(); QString Ename=ui->ename->text(); QString Enum=ui->epnum->text(); write<<"<"<<'\n'; write<<Username<<'\n'<<Password<<'\n'<<Name<<'\n'<<Email<<'\n'<<Address<<'\n'<<Age<<'\n'<<Gender<<'\n'<<Bloodtype<<'\n'<<Phone<<'\n'<<Ename<<'\n'<<Enum<<'\n'; write<<">"<<'\n'; file.flush(); file.close();
-
Read the file, let the user modify it's data, write it out again.
-
@Youssef_n9212 said in Editing a certain number of lines in a QFile:
and the second one is the password
I hope you store the password as hash, not clear text?!
Back to your question: do have one file per user or do you store data from many users in one file?
If you want to edit data in a file you need to rewrite the part of the file which is affected. If you have one file per data then simply read its content, modify and then overwrite the file with updated data. -
@jsulm said in Editing a certain number of lines in a QFile:
If you want to edit data in a file you need to rewrite the part of the file which is affected.
This may encourage the OP to think he can/try to update data/lines in his text file. As so many people come here thinking they can do. He needs to appreciate what @Christian-Ehrlicher has said:
Read the file, let the user modify it's data, write it out again.
@Youssef_n9212
If you wish to be able to just update one patient's record you must either use a database. or separate files as @jsulm said.
If you intend to append them to a text file as you have written then you have to read the whole text file and write the whole file out afresh with the change. -
@Youssef_n9212 Follow what @JonB @Christian-Ehrlicher wrote.
-
Hi,
If you really want to avoid using a database as already suggested in your other thread and here. Use a proper structure in your file like XML so that you can search through more easily.