QTextStreamを使ってファイルを1行ずつ読み込んで見る。
#include <QCoreApplication>
#include <QTimer>
#include <QFile>
#include <QTextStream>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTimer::singleShot(0, [&]()->void {
QFile file("c:/tmp/hoge.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
fprintf(stderr, "ERROR\n");
a.quit();
return;
}
QTextStream stream(&file);
while(!stream.atEnd()) {
QString line = stream.readLine();
qDebug() << line;
}
a.quit();
});
return a.exec();
}
QTextStreamのコンストラクタはQIODevice*を受け取るので、ファイルに限らずQIODeviceを継承したクラスで使用できると思う。
コメント