Unsolved How to quote a part of a line from a text file
-
@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...
-
- What have you tried ?
- And about file.readLine() feature ?
- 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.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(); }
-
@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
-
how is your Arduiono connected to the PC? by serial connection? then have a look at QSerialPort.