Help with QTextstream splitting "lang=2"
-
#include <QCoreApplication>
#include <QString>
#include <QTextStream>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QString qstr = "lang=2"; //! no spaces between lang and 2!) QTextStream ss(&qstr); QString lang; int kol_lang = 0; //ss >> lang >> dec >> kol_lang; //it works if there are spaces between lang and 2 //the following line is wrong - help! ss >> lang >> qSetPadChar('=') >> dec >> kol_lang; //separate setPadchar doesn't work
//I want to use the stream technique - I know about a QString split possibility
return a.exec();
}
-
@JonB, I want to achieve an easy thing -- to split the text "lang=2" using the stream techinique, without sscanf. Do the C++ text streams not allow to do such things? ;-) I can't believe it. Of course I don't insist on qSetPadChar -- just tell me how to split the "lang=2" that came from the text stream.
-
@qAlexKo said in Help with QTextstream splitting "lang=2":
I can't believe it.
Well that's up to you :) The simple answer is that you cannot do this "splitting on input" via
QTextStream
(it will only split input on whitespace, and the "pad char" is only for output). Or, to be more precise, not without subclassing it and writing your own override for>>
to do what you want. I refer you to e.g. https://stackoverflow.com/questions/27838186/qtextstream-read-a-string-until-tab.Using plain
QTextStream
you will need to usereadAll()
orreadLine()
and thenQString::split('=')
for your purpose. -
@JonB said in Help with QTextstream splitting "lang=2":
https://stackoverflow.com/questions/27838186/qtextstream-read-a-string-until-tab.
...
class MyTextStream : public QTextStream {...
I like it.Thank you
-
It works like this:
class MyTextStream : public QTextStream {
public:
QChar pad, skip;
MyTextStream(QString* str, QChar _pad, QChar _skip) : QTextStream(str) { pad = _pad; skip = _skip; }MyTextStream& operator>>(QString & str) {
QChar ch;
while (!atEnd()) {
QTextStream::operator >>(ch);
if (ch == skip) continue;
if (ch == pad) {
break;
}
str = str + ch;
}
return *this;
}
};int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QString qstr = "lang=2"; //! no spaces between lanf and 2!) MyTextStream ss(&qstr, '=', ' '); QString lang; int kol_lang = 0; ss >> lang >> dec >> kol_lang; return a.exec();
}
-
@qAlexKo
I have not tested, and you may say it does not apply to you because you will only ever have one line (in which case the whole overhead of using aQTextStream
seems quite pointless to me), but what does your new code do now on a file like, say:var1=value1 var2=value2
?
-
@JonB said in Help with QTextstream splitting "lang=2":
I have not tested, and you may say it does not apply to you because you will only ever have one line (in which case the whole overhead of using a QTextStream seems quite pointless to me), but what does your new code do now on a file like, say:
var1=value1
var2=value2MyTextStream is good for splitting a single text line only - it is the replacement for the unsafe sscanf -- you split the line and put values according their types.
var1=value1 is a text line that could be split in two QStrings var and value1.
but if you have var1=15.5 you can put 15.5 directly into the float var.QString qstr = "lang=2.23"; MyTextStream ss(&qstr, '=', ' '); QString lang; double kol_lang = 0; ss >> lang >> fixed >> kol_lang; QString qstr = "var1=value1"; MyTextStream ss(&qstr, '=', ' '); QString lang, qval; ss >> lang >> qval;