Last active
January 1, 2016 15:29
-
-
Save yamato8/8164720 to your computer and use it in GitHub Desktop.
Qt でファイルを読み込む
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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