Skip to content

Instantly share code, notes, and snippets.

@yamato8
Last active January 1, 2016 15:29
Show Gist options
  • Save yamato8/8164720 to your computer and use it in GitHub Desktop.
Save yamato8/8164720 to your computer and use it in GitHub Desktop.
Qt でファイルを読み込む
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
//QFile file( "D:\\qt\\qtTest\\data.txt" );//OK
QFile file( "data.txt" );// ビルドディレクトリの中のdata.txt
if (!file.open(QIODevice::ReadOnly))//読込のみでオープンできたかチェック
{
qDebug() << "can not open file." ;
return 0;
}
int n = 0;
QTextStream in( &file );
while (!in.atEnd()) {
QString line = in.readLine();
qDebug() << n << " : " << line ;// UTF-8 OK::UTF-8N は文字化け
n++;
}
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
//QFile file( "D:\\qt\\qtTest\\data.txt" );//OK
QFile file( "data.txt" );// ビルドディレクトリの中のdata.txt
if (!file.open(QIODevice::ReadOnly))//読込のみでオープンできたかチェック
{
qDebug() << "can not open file." ;
return 0;
}
QString str;
QTextStream in(&file);
str = in.readAll();//全文読込
qDebug() << str ;
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment