PDA

View Full Version : QTextStream, doubles and ints



SirhcdP
18th March 2009, 10:05
Hi every one.

I would appreciate it if some one can point me in the right direction. The problem i am having is, I have a line that i read from a file with doubles and ints in the line example


1 2 3 4.5 6.7 2.3

I want to put these numbers in ints and doubles. What i tried to do was.



int one, two, three;
double oned, twod threed;
QTextStream ssFile, line;
ssFile.setDevice(&file);
QString stringFile = ssFile.readLine();
line.setString(&stringFile);
line >> one >> two >> three >> oned >> twod >>threed;

Needles to say it did not work. is there an easy way in doing this.
Thanks in advance

Chris

^NyAw^
18th March 2009, 10:17
Hi,

Here you have "1 2 3 4.5 6.7 2.3" on a QSting, right?


QString stringFile = ssFile.readLine();
So then, you can use "space" as character separator using "QString::split"
Then you can do a loop to all QStringList elements converting to the right variable. Also, you can check if the QString element of the QStringList contains "." and if it contains it you can assign it to a double variable else to a int variable.

SirhcdP
18th March 2009, 10:46
Thank's I thought of doing it that way.
I just want to know if there are a way like int std c++ one can use std::stringstream and read it in the way I tried to with Qt.

^NyAw^
18th March 2009, 10:51
Hi,

Using QTextStream you can't because all is text, so then you have to parse the data.

SirhcdP
18th March 2009, 11:13
If one look at the example in the manual. I thought it would work that way.


By default, when reading numbers from a stream of text, QTextStream will automatically detect the number's base representation. For example, if the number starts with "0x", it is assumed to be in hexadecimal form. If it starts with the digits 1-9, it is assumed to be in decimal form, and so on. You can set the integer base, thereby disabling the automatic detection, by calling setIntegerBase(). Example:
QTextStream in("0x50 0x20");
int firstNumber, secondNumber;

in >> firstNumber; // firstNumber == 80
in >> dec >> secondNumber; // secondNumber == 0

char ch;
in >> ch; // ch == 'x'