【发布时间】:2013-05-21 11:34:18
【问题描述】:
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();
}
【问题讨论】:
-
你真正需要做什么?
-
我用 readLine() 读取了一行,我希望流中的光标回到行的开头
-
您希望得到什么?描述你想要得到的结果。
-
我想从文件中读取对象,并根据每行的第一个单词使用不同的对象构造函数
标签: c++ qt cursor readline qtextstream