読み込むファイルの準備:ビルドディレクトリの中に data.txt を作成する。
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(); | |
} |
D:\qt\build-qtTest-Desktop_Qt_5_1_1_MinGW_32bit-Debug\debug\qtTest.exe を起動中...
0 : "abcdefg"
1 : "あいうえお"
2 : "12345"
3 : "12345"
全文読み込み:
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(); | |
} |
D:\qt\build-qtTest-Desktop_Qt_5_1_1_MinGW_32bit-Debug\debug\qtTest.exe を起動中...
"abcdefg
あいうえお
12345
12345
0 件のコメント:
コメントを投稿