我需要从foo.txt读取字符串:
#include <QDebug>
#include <QFile>
#include <QStringList>
#include <QTextStream>
int main( int argc, char** argv )
{
QFile file("foo.txt");
if (!file.open(QIODevice::ReadOnly))
return 1;
QStringList stringList;
QTextStream textStream(&file);
textStream.readLine();
while (!textStream.atEnd())
stringList << textStream.readLine();
file.close();
qDebug() << stringList;
return 0;
}
文件已打开,但textStream始终为空。
答案 0 :(得分:1)
从您的评论中可以看出,可执行文件根本找不到文件,因为它们位于不同的位置。有多种方法可以解决这个问题,这取决于最终用例的含义。以下是一些解决问题的方法:
为了测试前两个选项中的任何一个都快速而简单,但如果你打算采取进一步措施,你可能会想要比这更好的东西。