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 Updated to NodeBB v4.3 + New Features

Read a file txt

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 3.0k Views 2 Watching
  • 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.
  • J.HilkJ J.Hilk

    @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 last edited by
    #14

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

    mrjjM 1 Reply Last reply
    0
    • L LatitudeFr

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

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #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
      2
      • mrjjM mrjj

        @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 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
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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
          0
          • mrjjM mrjj

            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 last edited by LatitudeFr
            #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

            mrjjM 1 Reply Last reply
            0
            • L LatitudeFr

              @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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #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.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on 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.

                mrjjM 1 Reply Last reply
                2
                • J.HilkJ J.Hilk
                  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
                  }
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #21

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

                  J.HilkJ 1 Reply Last reply
                  0
                  • mrjjM mrjj

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

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on 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
                    2
                    • J.HilkJ J.Hilk

                      @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 last edited by
                      #23

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

                      1 Reply Last reply
                      0

                      • Login

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