在readLine()
之后,如何将光标位置设置到行首?
使用seek()
和pos()
对我不起作用。
这是我的file.txt的样子:
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
这是我的代码:
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();
}
最佳答案
因此,您需要(反序列化)。
尝试做正确的事。
这是官方文档:http://qt-project.org/doc/qt-4.8/datastreamformat.html
这是示例:Serialization with Qt