Qt 4.7 學習紀錄~C++ 12 – QFile.mp4[利用QFile學習操作檔案讀寫等操作](YOUTUBE 教學備份) – jashliao部落格
2021/12/25
Qt 4.7 學習紀錄~C++ 12 – QFile.mp4[利用QFile學習操作檔案讀寫等操作](YOUTUBE 教學備份)
教學影片來源:https://www.youtube.com/watch?v=6KtOzh0StTc&list=PL2D1942A4688E9D63
Tool來源:http://qt.software.informer.com/download/
Github資源收藏:https://github.com/jash-git/Win_Qt_Learn
本教學內容,利用QFile學習操作檔案讀寫等操作
內容重點
建立文字檔的寫入(Write)/讀取(Read)函數,其中QFile只是則開關檔案,而要存取文字檔內容必須利用QTextStream
比較完整的檔案寫入和讀取的範例請參考project_code內Qt_012目錄的兩個CPP,此範例是完整的依序每次寫入和讀取都是以一行資料為單位的標準範例
//資料來源:https://openhome.cc/Gossip/Qt4Gossip/QTextStream.html
#include
#include
#include
using namespace std;
int main(int argc, char *argv[]) {
QString src(argv[1]);
QFile file(src);
if (!file.open(QIODevice::ReadOnly)) {
cerr << “Cannot open file for reading:”
<< qPrintable(file.errorString()) << endl;
return false;
}
QTextStream in(&file);
while (!in.atEnd()) {
cout << qPrintable(in.readLine()) << endl;
}
return true;
}
|
//資料來源:https://openhome.cc/Gossip/Qt4Gossip/QTextStream.html
#include
#include
#include
using namespace std;
int main(int argc, char *argv[]) {
QFile file(“data.txt”);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
cerr << “Cannot open file for writing:”
<< qPrintable(file.errorString()) << endl;
return false;
}
QTextStream out(&file);
out << “name\tscore” << endl;
out << “justin\t” << 95 << endl;
out << “momor\t” << 93 << endl;
out << “minnie\t” << 93 << endl;
return true;
}
|
Your browser does not support the HTML5 video tag.
熱門推薦
精彩推薦