After readLine()
, how to set cursor position to the beggining of a line?
Using seek()
and pos()
doesnt work for me.
Here is how my file.txt look like:
Object1 Some-name 2 3.40 1.50
Object2 Some-name 2 3.40 1.50 3.25
Object3 Some-name 2 3.40 1.50
Here's my code:
QFile file("file.txt");
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file);
while(!stream.atEnd()) {
qint64 posBefore = file.pos();
QString line = stream.readLine();
QStringList splitline = line.split(" ");
if(splitline.at(0) == "Object1") {
stream.seek(posBefore);
object1 tmp;
stream >> tmp;
tab.push_back(tmp);
}
if(splitline.at(0) == "Object2") {
stream.seek(posBefore);
object2 tmp;
stream >> tmp;
tab.push_back(tmp);
}
if(splitline.at(0) == "Object3") {
stream.seek(posBefore);
object3 tmp;
stream >> tmp;
tab.push_back(tmp);
}
}
file.close();
}
So, you need (de)serialization.
Try to do it right. Here is official documentation: http://qt-project.org/doc/qt-4.8/datastreamformat.html Here is example: Serialization with Qt