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. Read a file txt
Forum Update on Monday, May 27th 2025

Read a file txt

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 2.5k 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.
  • L LatitudeFr
    4 Aug 2022, 11:32

    @mrjj said in Read a file txt:

    while (!lire_data.atEnd())
    {
    qDebug() << lire_data.readLine();
    }

    thank you for your reply but it's the same result

    M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 4 Aug 2022, 11:44 last edited by
    #6

    @LatitudeFr
    hi
    How is that possible ?
    If you don't split to a list - it should show what it reads from the file directly and
    not as many as before.

    Do you call that code in a loop or something ?

    L 1 Reply Last reply 4 Aug 2022, 12:27
    0
    • M mrjj
      4 Aug 2022, 11:44

      @LatitudeFr
      hi
      How is that possible ?
      If you don't split to a list - it should show what it reads from the file directly and
      not as many as before.

      Do you call that code in a loop or something ?

      L Offline
      L Offline
      LatitudeFr
      wrote on 4 Aug 2022, 12:27 last edited by
      #7

      @mrjj the code :
      void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
      //ecrire les données***********************
      QFile file("data_sonde.txt");
      QStringList line_data ;

      if( file.open(QIODevice::Append)) {
      
          QTextStream data(&file);
          data<<data_sonde;
      
      
         file.close();
      

      //lire les données*************************
      QStringList line ;
      if (!file.open(QFile::ReadOnly | QFile::Text)){
      qDebug()<<"File not exists";
      }
      else {
      QTextStream lire_data(&file);
      while (!lire_data.atEnd())
      {

        qDebug() << lire_data.readLine();
        }
      

      file.close();

             }
      }
      

      }

      result :

      275c355e-42f9-4ff0-96f5-c0c7bf392f97-image.png

      file txt :

      935fb7c9-1553-4128-b14b-a675aa1c28e1-image.png

      M 1 Reply Last reply 4 Aug 2022, 12:40
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 4 Aug 2022, 12:34 last edited by mrjj 8 Apr 2022, 12:37
        #8

        Hi
        ok that i cant explain.

        Best guess is that you read data from a serial port and call save and load
        pr DataReady signal and hence we see it like this.

        When reading from serial port, all data might not come in one go. And it kinda looks like this but
        I can't understand how your text file then looks normal.
        (ahh you use append so the end result looks normal )

        can you show the code around where you call updateGUI_sonde ?

        L 1 Reply Last reply 4 Aug 2022, 12:55
        1
        • J Online
          J Online
          J.Hilk
          Moderators
          wrote on 4 Aug 2022, 12:37 last edited by
          #9

          @LatitudeFr I still think, there's a loop, or at least multiple calls to updateGUI_sonde

          add a debug() statement there, to test that

          void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
              qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
              //ecrire les données***********************
              QFile file("data_sonde.txt");
              QStringList line_data ;
          
              if( file.open(QIODevice::Append)) {
                 QTextStream data(&file);
                 data<<data_sonde;
          
                 file.close();
          
          //lire les données*************************
                 QStringList line ;
                 if (!file.open(QFile::ReadOnly | QFile::Text)) {
                        qDebug()<<"File not exists";
                 } else {
                      QTextStream lire_data(&file);
                      while (!lire_data.atEnd()) {
                              qDebug() << lire_data.readLine();
                      }
                      file.close();
                }
             }
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          L 1 Reply Last reply 4 Aug 2022, 12:44
          1
          • L LatitudeFr
            4 Aug 2022, 12:27

            @mrjj the code :
            void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
            //ecrire les données***********************
            QFile file("data_sonde.txt");
            QStringList line_data ;

            if( file.open(QIODevice::Append)) {
            
                QTextStream data(&file);
                data<<data_sonde;
            
            
               file.close();
            

            //lire les données*************************
            QStringList line ;
            if (!file.open(QFile::ReadOnly | QFile::Text)){
            qDebug()<<"File not exists";
            }
            else {
            QTextStream lire_data(&file);
            while (!lire_data.atEnd())
            {

              qDebug() << lire_data.readLine();
              }
            

            file.close();

                   }
            }
            

            }

            result :

            275c355e-42f9-4ff0-96f5-c0c7bf392f97-image.png

            file txt :

            935fb7c9-1553-4128-b14b-a675aa1c28e1-image.png

            M Offline
            M Offline
            mpergand
            wrote on 4 Aug 2022, 12:40 last edited by
            #10

            @LatitudeFr
            Salut,
            Utilise les chevrons <> pour rendre le code plus lisible.

            void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
            //ecrire les données***********************
            QFile file("data_sonde.txt");
            QStringList line_data ;
            
            if( file.open(QIODevice::Append)) {
            
                QTextStream data(&file);
                data<<data_sonde;
            

            You add/append data over again each time you execute your prog .

            1 Reply Last reply
            0
            • J J.Hilk
              4 Aug 2022, 12:37

              @LatitudeFr I still think, there's a loop, or at least multiple calls to updateGUI_sonde

              add a debug() statement there, to test that

              void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
                  qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
                  //ecrire les données***********************
                  QFile file("data_sonde.txt");
                  QStringList line_data ;
              
                  if( file.open(QIODevice::Append)) {
                     QTextStream data(&file);
                     data<<data_sonde;
              
                     file.close();
              
              //lire les données*************************
                     QStringList line ;
                     if (!file.open(QFile::ReadOnly | QFile::Text)) {
                            qDebug()<<"File not exists";
                     } else {
                          QTextStream lire_data(&file);
                          while (!lire_data.atEnd()) {
                                  qDebug() << lire_data.readLine();
                          }
                          file.close();
                    }
                 }
              }
              
              L Offline
              L Offline
              LatitudeFr
              wrote on 4 Aug 2022, 12:44 last edited by
              #11

              @J-Hilk thank you for your reply
              the result :
              a05a7025-381d-4b2b-b155-222b7a1c82b8-image.png
              can i send you the code by e-mail ?

              J 1 Reply Last reply 4 Aug 2022, 12:56
              0
              • M mrjj
                4 Aug 2022, 12:34

                Hi
                ok that i cant explain.

                Best guess is that you read data from a serial port and call save and load
                pr DataReady signal and hence we see it like this.

                When reading from serial port, all data might not come in one go. And it kinda looks like this but
                I can't understand how your text file then looks normal.
                (ahh you use append so the end result looks normal )

                can you show the code around where you call updateGUI_sonde ?

                L Offline
                L Offline
                LatitudeFr
                wrote on 4 Aug 2022, 12:55 last edited by
                #12

                @mrjj the code :

                connect(_sonde, &sonde::gotNewData_sonde, this, &MainWindow::updateGUI_sonde);
                

                signals:
                void gotNewData_sonde(QByteArray data_sonde );//prototype du signal
                void sonde::newData_sonde() {

                emit gotNewData_sonde(_sonde.readAll()); // lire tout les donnes dans le port serie
                

                }

                1 Reply Last reply
                1
                • L LatitudeFr
                  4 Aug 2022, 12:44

                  @J-Hilk thank you for your reply
                  the result :
                  a05a7025-381d-4b2b-b155-222b7a1c82b8-image.png
                  can i send you the code by e-mail ?

                  J Online
                  J Online
                  J.Hilk
                  Moderators
                  wrote on 4 Aug 2022, 12:56 last edited by
                  #13

                  @LatitudeFr no need, you need to accumulate your data.

                  like @mrjj said, your serial port(?) doesn't notify you when all data arrived, it does when any data arrived.

                  and you have to store that and decide when and how all data arrived.

                  from what you showed, I would say your message ends with a 0x2320aka # and space.

                  QByteArray accumulatedData; 
                  QByteArray expectedEnd=  QByteArray::fromHex("2320");
                  void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
                      qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
                      accumulatedData.append(data_sonde);
                   
                      if(!accumulatedData.endsWith(expectedEnd))
                         return;
                      //ecrire les données***********************
                      QFile file("data_sonde.txt");
                      QStringList line_data ;
                  
                      if( file.open(QIODevice::Append)) {
                         QTextStream data(&file);
                         data<<accumulatedData;
                  
                         file.close();
                         accumulatedData.clear();
                  
                  //lire les données*************************
                         QStringList line ;
                         if (!file.open(QFile::ReadOnly | QFile::Text)) {
                                qDebug()<<"File not exists";
                         } else {
                              QTextStream lire_data(&file);
                              while (!lire_data.atEnd()) {
                                      qDebug() << lire_data.readLine();
                              }
                              file.close();
                        }
                     }
                  }
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  L 1 Reply Last reply 4 Aug 2022, 13:04
                  2
                  • J J.Hilk
                    4 Aug 2022, 12:56

                    @LatitudeFr no need, you need to accumulate your data.

                    like @mrjj said, your serial port(?) doesn't notify you when all data arrived, it does when any data arrived.

                    and you have to store that and decide when and how all data arrived.

                    from what you showed, I would say your message ends with a 0x2320aka # and space.

                    QByteArray accumulatedData; 
                    QByteArray expectedEnd=  QByteArray::fromHex("2320");
                    void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
                        qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
                        accumulatedData.append(data_sonde);
                     
                        if(!accumulatedData.endsWith(expectedEnd))
                           return;
                        //ecrire les données***********************
                        QFile file("data_sonde.txt");
                        QStringList line_data ;
                    
                        if( file.open(QIODevice::Append)) {
                           QTextStream data(&file);
                           data<<accumulatedData;
                    
                           file.close();
                           accumulatedData.clear();
                    
                    //lire les données*************************
                           QStringList line ;
                           if (!file.open(QFile::ReadOnly | QFile::Text)) {
                                  qDebug()<<"File not exists";
                           } else {
                                QTextStream lire_data(&file);
                                while (!lire_data.atEnd()) {
                                        qDebug() << lire_data.readLine();
                                }
                                file.close();
                          }
                       }
                    }
                    
                    L Offline
                    L Offline
                    LatitudeFr
                    wrote on 4 Aug 2022, 13:04 last edited by
                    #14

                    @J-Hilk it works very well thank you very much but please how I can read just the last line

                    M 1 Reply Last reply 4 Aug 2022, 13:13
                    0
                    • L LatitudeFr
                      4 Aug 2022, 13:04

                      @J-Hilk it works very well thank you very much but please how I can read just the last line

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 4 Aug 2022, 13:13 last edited by mrjj 8 Apr 2022, 13:13
                      #15

                      @LatitudeFr

                      Hi
                      it doesn't work like that :)

                      Say you send "This is my line" from the board.

                      then you get more than one read signal and data comes like

                      Thi
                      is m
                      y li
                      ne

                      so you need to add them together to make a line of it.
                      In this case we know we got all data when we see #

                      there is no last line. it comes in bytes and we make a line from that.

                      L 1 Reply Last reply 4 Aug 2022, 13:44
                      2
                      • M mrjj
                        4 Aug 2022, 13:13

                        @LatitudeFr

                        Hi
                        it doesn't work like that :)

                        Say you send "This is my line" from the board.

                        then you get more than one read signal and data comes like

                        Thi
                        is m
                        y li
                        ne

                        so you need to add them together to make a line of it.
                        In this case we know we got all data when we see #

                        there is no last line. it comes in bytes and we make a line from that.

                        L Offline
                        L Offline
                        LatitudeFr
                        wrote on 4 Aug 2022, 13:44 last edited by
                        #16

                        @mrjj with this code the problem is solved but I would need to read every time I press the button just the last line and not all the lines
                        the code :
                        QByteArray accumulatedData;
                        QByteArray expectedEnd= QByteArray::fromHex("2320");
                        void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
                        // qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
                        accumulatedData.append(data_sonde);

                        if(!accumulatedData.endsWith(expectedEnd))
                           return;
                        
                        //ecrire les données***********************
                        QFile file("data_sonde.txt");
                        QStringList line_data ;
                        
                        if( file.open(QIODevice::Append)) {
                           QTextStream data(&file);
                           data<<accumulatedData;
                        
                           file.close();
                           accumulatedData.clear();
                        

                        //lire les données*************************
                        QStringList line ;
                        if (!file.open(QFile::ReadOnly | QFile::Text)) {
                        qDebug()<<"File not exists";
                        } else {
                        QTextStream lire_data(&file);
                        while (!lire_data.atEnd()) {
                        line =lire_data.readLine().split(' ');
                        if(!line.contains(("para")))
                        {
                        if(!line.contains(("data")))
                        {
                        if(!line.contains(("#")))
                        {

                                                   bool ok;
                                                   float ph =line.at(0).toDouble(&ok);
                                                   if(!ok)
                                                   continue;
                                                   qDebug()<<ph;
                                                   ui->sonde_label->setNum(ph);
                        
                                         qDebug() << line;
                                               }
                                        }
                                    }
                                }
                                file.close();
                          }
                        

                        }
                        }

                        the file.txt
                        875033b3-1f5a-47f4-af8a-1028e228595a-image.png

                        the result
                        6394b266-46f8-4a2a-a90d-520e91c30737-image.png

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 4 Aug 2022, 13:49 last edited by
                          #17

                          Hi
                          Since you now wait until you see #

                          if(!accumulatedData.endsWith(expectedEnd))
                             return;
                          

                          then you might want to use

                          file.open(QIODevice::Read | QIODevice::Truncate | QIODevice::Text);
                          and not
                          file.open(QIODevice::Append)

                          so you only save the complete line once. Else if you test more than once and don't delete the file between,
                          it will have more than 1 data line /set. Like you show in the notepad image.

                          L 1 Reply Last reply 4 Aug 2022, 13:56
                          0
                          • M mrjj
                            4 Aug 2022, 13:49

                            Hi
                            Since you now wait until you see #

                            if(!accumulatedData.endsWith(expectedEnd))
                               return;
                            

                            then you might want to use

                            file.open(QIODevice::Read | QIODevice::Truncate | QIODevice::Text);
                            and not
                            file.open(QIODevice::Append)

                            so you only save the complete line once. Else if you test more than once and don't delete the file between,
                            it will have more than 1 data line /set. Like you show in the notepad image.

                            L Offline
                            L Offline
                            LatitudeFr
                            wrote on 4 Aug 2022, 13:56 last edited by LatitudeFr 8 Apr 2022, 14:03
                            #18

                            @mrjj thank your for your reply but i need file.open(QIODevice::Append) to write on the file
                            for the first time it reads only one data since in our file there is only one data but when I press the button for the second time it reads 2 data since we have 2 data in the file but I would need to read just the last data

                            for the first time
                            1.PNG
                            2.PNG

                            for the second time
                            3.PNG
                            4.PNG

                            M 1 Reply Last reply 4 Aug 2022, 14:03
                            0
                            • L LatitudeFr
                              4 Aug 2022, 13:56

                              @mrjj thank your for your reply but i need file.open(QIODevice::Append) to write on the file
                              for the first time it reads only one data since in our file there is only one data but when I press the button for the second time it reads 2 data since we have 2 data in the file but I would need to read just the last data

                              for the first time
                              1.PNG
                              2.PNG

                              for the second time
                              3.PNG
                              4.PNG

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 4 Aug 2022, 14:03 last edited by mrjj 8 Apr 2022, 14:04
                              #19

                              @LatitudeFr

                              Why ?

                              it will only write a full line since we wait until we see #

                              so when your load part runs, then it can load the full line.

                              Using append when opening the files allows appending more to it next time.
                              but unless board sent many different data lines and you only want the last one, I'm not sure why
                              you need append ?

                              (update) Ahh. you do want to send more than one data line and store all in a file but then later
                              only use the last data ?

                              1 Reply Last reply
                              0
                              • J Online
                                J Online
                                J.Hilk
                                Moderators
                                wrote on 4 Aug 2022, 14:20 last edited by
                                #20
                                QByteArray accumulatedData; 
                                QByteArray expectedEnd=  QByteArray::fromHex("2320");
                                void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
                                    qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
                                    accumulatedData.append(data_sonde);
                                 
                                    if(!accumulatedData.endsWith(expectedEnd))
                                       return;
                                    //ecrire les données***********************
                                    QFile file("data_sonde.txt");
                                    QStringList line_data ;
                                
                                    if( file.open(QIODevice::Append)) {
                                       QTextStream data(&file);
                                       data<<accumulatedData;
                                
                                       file.close();
                                       
                                //lire les données*************************
                                       QStringList line ;
                                       if (!file.open(QFile::ReadOnly | QFile::Text)) {
                                              qDebug()<<"File not exists";
                                       } else {
                                            QTextStream lire_data(&file);
                                            lire_data.seek(file.size() - accumulatedData.size());
                                            while (!lire_data.atEnd()) {
                                                    qDebug() << lire_data.readLine();
                                            }
                                            file.close();
                                      }
                                   }
                                   accumulatedData.clear(); //We now clear at the very end
                                }
                                

                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                M 1 Reply Last reply 4 Aug 2022, 14:21
                                2
                                • J J.Hilk
                                  4 Aug 2022, 14:20
                                  QByteArray accumulatedData; 
                                  QByteArray expectedEnd=  QByteArray::fromHex("2320");
                                  void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
                                      qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
                                      accumulatedData.append(data_sonde);
                                   
                                      if(!accumulatedData.endsWith(expectedEnd))
                                         return;
                                      //ecrire les données***********************
                                      QFile file("data_sonde.txt");
                                      QStringList line_data ;
                                  
                                      if( file.open(QIODevice::Append)) {
                                         QTextStream data(&file);
                                         data<<accumulatedData;
                                  
                                         file.close();
                                         
                                  //lire les données*************************
                                         QStringList line ;
                                         if (!file.open(QFile::ReadOnly | QFile::Text)) {
                                                qDebug()<<"File not exists";
                                         } else {
                                              QTextStream lire_data(&file);
                                              lire_data.seek(file.size() - accumulatedData.size());
                                              while (!lire_data.atEnd()) {
                                                      qDebug() << lire_data.readLine();
                                              }
                                              file.close();
                                        }
                                     }
                                     accumulatedData.clear(); //We now clear at the very end
                                  }
                                  
                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 4 Aug 2022, 14:21 last edited by
                                  #21

                                  @J-Hilk
                                  Cool but wont he just get the last # and not the data line ?

                                  J 1 Reply Last reply 4 Aug 2022, 14:30
                                  0
                                  • M mrjj
                                    4 Aug 2022, 14:21

                                    @J-Hilk
                                    Cool but wont he just get the last # and not the data line ?

                                    J Online
                                    J Online
                                    J.Hilk
                                    Moderators
                                    wrote on 4 Aug 2022, 14:30 last edited by
                                    #22

                                    @mrjj he shouldn't, wee accumulate the whole message, before we actually write:

                                    void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
                                        qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
                                        accumulatedData.append(data_sonde);
                                     
                                        if(!accumulatedData.endsWith(expectedEnd))
                                           return;
                                    

                                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                    Q: What's that?
                                    A: It's blue light.
                                    Q: What does it do?
                                    A: It turns blue.

                                    L 1 Reply Last reply 4 Aug 2022, 14:49
                                    2
                                    • J J.Hilk
                                      4 Aug 2022, 14:30

                                      @mrjj he shouldn't, wee accumulate the whole message, before we actually write:

                                      void MainWindow::updateGUI_sonde(QByteArray data_sonde) {
                                          qDebug() << Q_FUNC_INFO << data_sonde.toHex(' '); // <------ this one
                                          accumulatedData.append(data_sonde);
                                       
                                          if(!accumulatedData.endsWith(expectedEnd))
                                             return;
                                      
                                      L Offline
                                      L Offline
                                      LatitudeFr
                                      wrote on 4 Aug 2022, 14:49 last edited by
                                      #23

                                      @J-Hilk @mrjj thank you it works very well

                                      1 Reply Last reply
                                      0

                                      15/23

                                      4 Aug 2022, 13:13

                                      • Login

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