[SOLVED] QTextStream to read a file
-
Hi!
I want to read a .txt file using Qt. The file has this content:
height: 184.2
lenght: 21.4
color: 47
...I only want to store these values (184.2, 21.4, 47) in my variables. By now, I'm reading the file line by line and I'm "converting" those strings into float numbers. This is what I'm doing:
@myString = myStream.readLine();
myString.remove(QRegExp("[abcdefghijklmnopqrstuvwxyz_: ]"));
myFloat = myString.toFloat();@The problem comes when I try to read a number in scientific notation (for example 5e-6). My code doesn't work because it removes the letter "e". ¿How can I solve this? I'm sure there's a smarter way of managing all this.
Thanks in advance
-
Hi, and welcome to the Qt Dev Net!
If every line has the same format, call
@
myString.split(": ");
@This will, for example, split your first line into a list of 2 strings: "height" and "184.2". Then, you can discard the first string and keep the second.
You can find many more useful functions in the "QString documentation":http://qt-project.org/doc/qt-5/QString.html.
-
I.e. it states specifically that the 5e-6 format is "supported":http://qt-project.org/doc/qt-4.8/qstring.html#toDouble
So as JKSH maintains the 'e' in the notation, this should be a working solution.
Soraltan
P.S.: If you should have problems with leading or tailing white spaces use "QString::trimmed()":http://qt-project.org/doc/qt-4.8/qstring.html#trimmed
-
Hii try this,
QFile mFile("d:/a.txt");
mFile.open(QIODevice::Text|QIODevice::ReadOnly);
QTextStream myStream(&mFile);
while(!myStream.atEnd())
{
QString myString = myStream.readLine();
//qDebug()<<myString;
myString=myString.mid(myString.lastIndexOf(":")+2,myString.length());
//qDebug()<<"222 : " << myString;
double myDbl = myString.toDouble();
qDebug()<<myDbl;
}
a.close();
-
[quote author="IamSumit" date="1400063233"]
myString=myString.mid(myString.lastIndexOf(":")+2,myString.length());[/quote]Why so complicated? That's what QString::split() is for -
[quote author="JKSH" date="1400065270"][quote author="IamSumit" date="1400063233"]
myString=myString.mid(myString.lastIndexOf(":")+2,myString.length());[/quote]Why so complicated? That's what QString::split() is for[/quote]Yeah You are right ..but in some cases it will be failed .
Like to obtain Ratio on a file
Length : width : 1:2
isn't it? -
Like always when doing some kind of parsing you can only provide a working solution for a given set of 'language constraints'. Such as "Does only one ':' occur in a line?" JKSH's solution assumes that.
I would understand your example in a way, that '1:2' should be returned, while yours solution above would return only the 2, wouldn't it?
Anyway, @PabloAG if considers your problem to be solved, please mark the thread by prepending a [Solved] in the title.
Best
Soraltan
-
[quote author="IamSumit" date="1400134234"]Yeah You are right ..but in some cases it will be failed .
Like to obtain Ratio on a file
Length : width : 1:2
isn't it?
[/quote]That's why I said "If every line has the same format". :) PabloAG said he has floating point numbers, not ratios.[quote author="PabloAG" date="1400109977"]Thanks! Split worked perfectly.[/quote]You're welcome. Happy coding!