[英]Reading strings from file. QTextStream does not read file
我需要從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始終為空。
從您的評論看來,可執行文件根本找不到文件,因為它們位於不同的位置。 有多種解決方法,這取決於最終用例的含義。 您可以通過以下幾種方法解決此問題:
測試前兩個選項中的任何一個都是快速簡便的,但是如果您打算進一步進行測試,則可能會需要比這更好的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.