PDA

View Full Version : get data from readLine()



sydjung
11th October 2011, 09:07
Hi guys,

I have a question regarding get data from readLine() function.

Currently, my function read line by line from a text file.
Each line has two numbers with a space between them. (eg. 234.23 454.32).
Is there a way to get those two number separately?

Here is my code.


bool getInfo(QString fileName) {
QFile myFile(fileName);
if(!myFile.exists()){
qDebug()<<"The file"<<myFile.fileName()<<"does not exist.";
return false;
}
if(!myFile.open(QIODevice::ReadOnly | QIODevice::Text)){
qFatal("Could not open the file");
return false;
}

QTextStream st(&myFile);

while (!st.atEnd()){
QString text;
text=st.readLine();
qDebug() << text;


In the code each line is stored in text and displayed by qDebug(). I would like to also display each numbers in a line sperately like x value is 234.23 and y value is 454.32.

Any idea?

wysota
11th October 2011, 09:42
while(!st.atEnd()) {
qreal one, two;
st >> one >> two;
qDebug() << one << two;
}

sydjung
11th October 2011, 11:44
while(!st.atEnd()) {
qreal one, two;
st >> one >> two;
qDebug() << one << two;
}

Thank you so much for your helping. Now it displays each number.
But it displays 0 0 at the end. Do you know what is the cause?

wysota
11th October 2011, 12:51
Most likely you have a newline character as the last character of the file hence atEnd() returns false but it is not possible to read values.