Qt使用Qtextstream進行換行操作

2021-08-02 20:01:21 字數 1049 閱讀 2295

使用qtextstream向txt檔案輸出換行時,需要使用qiodevice

::text

標誌。

官方文件對qiodevice

::text

的解釋:

when reading, the end-of-line terminators are translated to '\n'. when writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for win32.

簡而言之,該標誌指示在讀寫過程中要對end-of-line進行轉換。

例子

(1)不使用qiodevice

::text

[cpp]view plain

copy

print?

#include 

#include 

intmain(

intargc, 

char

*argv)  

return

a.exec();  

}  

輸出:

可見輸出換行達不到效果。

(2)不使用qiodevice

::text

[cpp]view plain

copy

print?

#include 

#include 

intmain(

intargc, 

char

*argv)  

return

a.exec();  

}  

效果:

**:

QTextStream的使用 讀寫檔案

注意,在用qtextstream讀寫檔案時,必須先把檔案開啟,然後用完以後把檔案關閉,如果不想檔案儲存在硬碟上,最後還要把檔案刪除,如下 所示 qfile fjson qstring fromstdstring strfilepath strjsonfilename qfile fjsonstyle...

QTextStream 讀取檔案內容

qfile file file.txt if file.open qiodevice readonly qiodevice text return qtextstream in file 1.qstring qtextstream read qint64 maxlen 讀取txt檔案maxlen個字...

QTextStream讀取檔案內容

qfile file file.txt if file.open qiodevice readonly qiodevice text return qtextstream in file 1.qstring qtextstream read qint64 maxlen 讀取txt檔案maxlen個字...