文件的讀寫是很多應用程序具有的功能,甚至某些應用程序就是圍繞著某一種格式文件的處 理而開發(fā)的,所以文件讀寫是應用程序開發(fā)的一個基本功能。
Qt文件操作有兩種方式,第一種使用QFile類的IODevice讀寫功能直接讀寫,第二種是利用 QFile和QTextStream結(jié)合起來,用流的方式進行文件讀寫。
第一種,利用QFile中的相關函數(shù),實現(xiàn)對文件的讀寫操作,QFile會調(diào)用IODevice設備,從而實現(xiàn)文件讀寫。
QT基本文件讀寫
通過QFile實現(xiàn)文本文件讀寫操作.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include <QCoreApplication> #include <iostream> #include <QFile> #include <QString> #include <QTextStream> // 一次讀入所有文本 bool ReadFileOnly( const QString &file_path) { QFile ptr(file_path); // 文件是否存在 if (!ptr.exists()) { return false ; } // 文件是否打開 /* ReadOnly 以只讀方式打開 WriteOnly 以只寫方式打開 ReadWrite 讀寫方式打開 Append 以追加方式打開 Truncate 以截取方式打開(原有內(nèi)容被清空) Text 以文件方式打開 */ if (!ptr.open(QIODevice::ReadWrite | QIODevice::Text)) { return false ; } QString text = ptr.readAll(); std::cout << text.toStdString() << std::endl; ptr.close(); } // 追加寫入文本 bool WriteFileOnly( const QString &file_path, QString save) { // 如果參數(shù)為空則返回假 if (file_path.isEmpty() && save.isEmpty()) { return false ; } QFile ptr(file_path); if (!ptr.open(QIODevice::Append | QIODevice::Text)) { return false ; } QByteArray str_bytes = save.toUtf8(); ptr.write(str_bytes,str_bytes.length()); ptr.close(); return true ; } |
QFile::open() 函數(shù)打開文件時需要傳遞 QIODevice::OpenModeFlag 枚舉類型的參數(shù),決定文件以什么方式打開,QIODevice::OpenModeFlag 類型的主要取值如下:
QIODevice::ReadOnly:以只讀方式打開文件,用于載入文件。
QIODevice::WriteOnly:以只寫方式打開文件,用于保存文件。
QIODevice::ReadWrite:以讀寫方式打開。
QIODevice::Append:以添加模式打開,新寫入文件的數(shù)據(jù)添加到文件尾部。
QIODevice::Truncate:以截取方式打開文件,文件原有的內(nèi)容全部被刪除。
QIODevice::Text:以文本方式打開文件,讀取時“\n”被自動翻譯為換行符,寫入時字符串結(jié)束符會自動翻譯為系統(tǒng)平臺的編碼,如 Windows 平臺下是“\r\n”。
這些取值可以組合,例如 QIODevice::ReadOnly | QIODevice::Text 表示以只讀和文本方式打開文件。
QTextStream 實現(xiàn)流讀寫
直接使用流寫入,可以使用<< 運算符,方便的寫入文本。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include <QCoreApplication> #include <iostream> #include <QFile> #include <QString> #include <QTextStream> #include <QTextCodec> // 計算文件行數(shù) qint32 get_file_count( const QString &file_path) { QFile ptr(file_path); qint32 count = 0; if (ptr.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&ptr); // 自動檢測unicode編碼,顯示中文 in.setAutoDetectUnicode( true ); while (!in.atEnd()) { QString line = in.readLine(); std::cout << line.toStdString() << std::endl; count = count +1; } return count; } return 0; } // 追加寫入數(shù)據(jù) bool write_file_stream( const QString &file_path, QString save) { QFile ptr(file_path); if (ptr.open(QIODevice::Append | QIODevice::Text)) { QTextStream in(&ptr); in << save; } ptr.close(); return true ; } int main( int argc, char *argv[]) { QCoreApplication a(argc, argv); // 設置編碼 QTextCodec *codec = QTextCodec::codecForName( "utf-8" ); QTextCodec::setCodecForLocale(codec); // 流寫入 write_file_stream( "d://test.txt" , "hello lyshark" ); write_file_stream( "d://test.txt" , "你好,世界" ); // 取文本長度 qint32 count = get_file_count( "d://test.txt" ); std::cout << "line = > " << count << std::endl; return a.exec(); } |
到此這篇關于C/C++ Qt 基本文件讀寫的基本使用(2種實現(xiàn))的文章就介紹到這了,更多相關Qt 文件讀寫內(nèi)容請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/LyShark/p/15433616.html