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



  • Hello everybody,

    I need to load a text file saved in my computer, and then I need to set all the parameters contained in this file to make the project work.

    I explain :

    I have an Arduino Mega 2560 containing a program that I coded earlier with Arduino IDE. I want to use the terminal from Qt Creator to actually load a text file containing lines such as :

    com= com5 # serial port configuration
    baud= 9600 # baudrate
    # Config Default 04/07/2018
    init 3 #

    So when the file is loaded, i need to write a switch case containing :

    if the line starts with "com= " , set the serial port to com5
    if the line starts with "baud= " , then set the baudrate to 9600
    if the line starts with "init " , configure the initialisation (done in Arduino code)

    My tutor told me that I can use switch such as Arduino IDE but this is a word switch case, not a letter switch case.

    I am really a noob as this is an Internship that I am doing, so I hope I made myself be understood :)

    Thank you guys



  • 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



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

    Thanks



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



  • @lolilol78

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


  • @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();
    }
    


  • @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();
        }
    

  • Moderators

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



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



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

    I will come back to you ,

    Thanks again



  • 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


  • Qt Champions 2017

    @lolilol78

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.