Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Editing a certain number of lines in a QFile
QtWS25 Last Chance

Editing a certain number of lines in a QFile

Scheduled Pinned Locked Moved Unsolved General and Desktop
qfiletext file
8 Posts 5 Posters 699 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Youssef_n9212
    wrote on 20 May 2022, 07:51 last edited by
    #1

    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();
    
    J 1 Reply Last reply 20 May 2022, 07:56
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 20 May 2022, 07:54 last edited by
      #2

      Read the file, let the user modify it's data, write it out again.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • Y Youssef_n9212
        20 May 2022, 07:51

        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();
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 20 May 2022, 07:56 last edited by
        #3

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        J Y 2 Replies Last reply 20 May 2022, 07:59
        0
        • J jsulm
          20 May 2022, 07:56

          @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.

          J Offline
          J Offline
          JonB
          wrote on 20 May 2022, 07:59 last edited by JonB
          #4

          @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.

          J 1 Reply Last reply 20 May 2022, 08:04
          0
          • J jsulm
            20 May 2022, 07:56

            @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.

            Y Offline
            Y Offline
            Youssef_n9212
            wrote on 20 May 2022, 08:03 last edited by
            #5

            @jsulm I store the data of all users in one file where the data of each user is stored between the < > characters

            J 1 Reply Last reply 20 May 2022, 08:05
            0
            • J JonB
              20 May 2022, 07:59

              @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.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 20 May 2022, 08:04 last edited by
              #6

              @JonB Yes, I expressed myself in a wrong way :-)

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Y Youssef_n9212
                20 May 2022, 08:03

                @jsulm I store the data of all users in one file where the data of each user is stored between the < > characters

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 20 May 2022, 08:05 last edited by jsulm
                #7

                @Youssef_n9212 Follow what @JonB @Christian-Ehrlicher wrote.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 20 May 2022, 19:20 last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  2/8

                  20 May 2022, 07:54

                  topic:navigator.unread, 6
                  • Login

                  • Login or register to search.
                  2 out of 8
                  • First post
                    2/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved