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. How to quote a part of a line from a text file
Forum Updated to NodeBB v4.3 + New Features

How to quote a part of a line from a text file

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 2.2k 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.
  • VRoninV VRonin

    regular expressions are your friends.
    Load the file in a QString then you can use QRegularExpression capture to extract what you want. For example:

    QFile paramFile("mifile.txt");
    if(paramFile.open(QFile::ReadOnly | QFile::Text)){
    QTextStream fileStream(&paramFile);
    const QString fileString = fileStream.readAll();
    const QRegularExpression baudExpression(R"***(^\s*baud\s*=\s*(\d+))***",QRegularExpression::CaseInsensitiveOption);
    const QRegularExpressionMatch baudMatch = baudExpression.match(fileString);
    if(baudMatch.hasMatch()){
    qDebug() << "baud rate: " << baudMatch.capturedRef(1).toInt();
    }
    }
    
    

    https://regex101.com/ is a huge help is regexp syntax handling

    L Offline
    L Offline
    lolilol78
    wrote on last edited by
    #3

    @VRonin Thanks for your answer, I will try to understand this as I am really a novice at this ahah.

    Thanks

    1 Reply Last reply
    0
    • VRoninV VRonin

      regular expressions are your friends.
      Load the file in a QString then you can use QRegularExpression capture to extract what you want. For example:

      QFile paramFile("mifile.txt");
      if(paramFile.open(QFile::ReadOnly | QFile::Text)){
      QTextStream fileStream(&paramFile);
      const QString fileString = fileStream.readAll();
      const QRegularExpression baudExpression(R"***(^\s*baud\s*=\s*(\d+))***",QRegularExpression::CaseInsensitiveOption);
      const QRegularExpressionMatch baudMatch = baudExpression.match(fileString);
      if(baudMatch.hasMatch()){
      qDebug() << "baud rate: " << baudMatch.capturedRef(1).toInt();
      }
      }
      
      

      https://regex101.com/ is a huge help is regexp syntax handling

      L Offline
      L Offline
      lolilol78
      wrote on last edited by
      #4

      @VRonin said in How to quote a part of a line from a text file:

      QRegularExpression

      The problem is that my tutor wants me to use a switch case that will look in the text if the line starts by.... etc...

      jsulmJ 1 Reply Last reply
      0
      • KillerSmathK Offline
        KillerSmathK Offline
        KillerSmath
        wrote on last edited by
        #5

        @lolilol78

        1. What have you tried ?
        2. And about file.readLine() feature ?
        3. Do you know what is Regular Expression ?

        @Computer Science Student - Brazil
        Web Developer and Researcher
        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

        L 1 Reply Last reply
        1
        • KillerSmathK KillerSmath

          @lolilol78

          1. What have you tried ?
          2. And about file.readLine() feature ?
          3. Do you know what is Regular Expression ?
          L Offline
          L Offline
          lolilol78
          wrote on last edited by
          #6

          @KillerSmath Hello Killer,

          I am really a huge noob so I dont even know where to start actually, I dont know what Regular expression is, coding in Arduino was a bit easier I think.

          For the moment the code is like this :

          #include <QCoreApplication>
          #include <QFile>
          #include <QString>
          #include <QDebug>
          #include <QTextStream>
          
          void read(QString filename)
          {
          
          
              QFile inputFile(filename);
              if (inputFile.open(QIODevice::ReadOnly))
              {
                 QTextStream in(&inputFile);
                 while (!in.atEnd())
                 {
                    QString line = in.readLine();
                    qDebug() << line;
          
          
                 }
                 inputFile.close();
              }
          }
          
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
          
              QString filename = "test.txt";
          
              read(filename);
          
              return a.exec();
          }
          
          1 Reply Last reply
          1
          • KillerSmathK Offline
            KillerSmathK Offline
            KillerSmath
            wrote on last edited by
            #7

            @lolilol78
            Regular expression basically is a feature to find words specificed by pattern in a text.

            You need to insert a pattern to say how the regular expression must to capture the words or parts of your text.
            This pattern uses Regular Expression Language.

            Regular Expression Example

            Notice that in this example, the regular expression captures 2 words specified by pattern "(word)= (word)"

            In Qt:

            #include <QDebug>
            #include <QFile>
            #include <QRegularExpression>
            #include <QRegularExpressionMatch>
            
            // main function
            
                QFile file("Your File Location");
            
                const QRegularExpression regex("^(\\w+)= (\\w+).*$");
            
                if(file.open(QFile::ReadOnly | QFile::Text)){
                    QTextStream in(&file);
            
                    while(!in.atEnd()){
                        const QString lineString = in.readLine(); // read line
                        const QRegularExpressionMatch match = regex.match(lineString); // apply the pattern on Text
            
                        if(match.hasMatch()){ // if captured the words
                            qDebug() << "Full Capture: " << match.captured(0);
                            qDebug() << "Captured Words: " << match.captured(1) << match.captured(2);
                        }
                    }
                    file.close();
                }
            

            @Computer Science Student - Brazil
            Web Developer and Researcher
            “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

            1 Reply Last reply
            4
            • L lolilol78

              @VRonin said in How to quote a part of a line from a text file:

              QRegularExpression

              The problem is that my tutor wants me to use a switch case that will look in the text if the line starts by.... etc...

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @lolilol78 said in How to quote a part of a line from a text file:

              The problem is that my tutor wants me to use a switch case that will look in the text if the line starts by.... etc...

              Please take a look at documentation: http://doc.qt.io/qt-5/qstring.html#startsWith
              There is a bunch of startsWith() methods.
              But the thing is: switch is based on enumerations/numbers (enum, integers) not boolean (only for two cases, but you have 3). So, I'm not sure what your tutor really want you to do.

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

              1 Reply Last reply
              3
              • J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #9

                switch & case is really just a glorified If()else If()else If()....

                Your best option would be something like this:

                QString line = in.readLine();
                if(line.startsWith("com"){
                ....
                } else if(line.startsWith("baud")){
                    ....
                } else if() ...
                

                But, If you really, really have to use switch&case, you could, untested from my side, take the first few chars of your line string, cast them as a big enough integer type and switch over that.


                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.

                1 Reply Last reply
                2
                • L Offline
                  L Offline
                  lolilol78
                  wrote on last edited by
                  #10

                  Thank you everyone for your replies, we made another way working around,

                  I will come back to you ,

                  Thanks again

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lolilol78
                    wrote on last edited by lolilol78
                    #11

                    Hello everybody,
                    I need to send the lines from the text file to be read on Arduino IDE code.

                    For example, the line "init 2" will be extracted on Qt via the code already written on Qt and then "I 2" needs to be sent to Arduino code to start a function that I already made on Arduino . The function works when Arduino receives "I 2".
                    But I dont know actually how to send a line from the Terminal of Qt that has already been extracted from the text file in Qt.

                    By the way, everything has already been extracted from the text file, the code on Qt Creator works, it converts init 2 into I 2, I just need to know how to send this line to be checked on Arduino IDE.

                    Thanks for your help

                    aha_1980A 1 Reply Last reply
                    0
                    • L lolilol78

                      Hello everybody,
                      I need to send the lines from the text file to be read on Arduino IDE code.

                      For example, the line "init 2" will be extracted on Qt via the code already written on Qt and then "I 2" needs to be sent to Arduino code to start a function that I already made on Arduino . The function works when Arduino receives "I 2".
                      But I dont know actually how to send a line from the Terminal of Qt that has already been extracted from the text file in Qt.

                      By the way, everything has already been extracted from the text file, the code on Qt Creator works, it converts init 2 into I 2, I just need to know how to send this line to be checked on Arduino IDE.

                      Thanks for your help

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by aha_1980
                      #12

                      @lolilol78

                      how is your Arduiono connected to the PC? by serial connection? then have a look at QSerialPort.

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      2

                      • Login

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