QFile讀取檔案亂碼問題

2021-10-03 18:32:40 字數 492 閱讀 5539

2020-3-11

使用qt的qfile開啟檔案時, 中文輸出亂碼, 知道是編碼的問題, 由於第一次接觸qt,所有不太清楚如何解決, 查詢一番, 認識了乙個類qtextcodec,感覺這個比較好, 記在這裡,方便日後複習

解決方法

qtextcodec codec = qtextcodec::

codecforname

("gbk");

qfile file

("***");

//***為要讀取的檔案路徑

file.

open(*

***)

;//開啟檔案,***為開啟模式

qbyetearray text = file.

readall()

;//讀取所有內容

qstring strtext = codec.

tounicode

(text)

;

QFile讀取文字檔案中文亂碼處理

在使用qfile讀取文字檔案時,如果檔案內容是gbk 包括gb2313 格式的,則會顯示為亂碼 使用qtextstream指明待讀取的檔案的編碼格式,如下 qtextstream in file in.setcodec gbk 設定檔案的編碼格式為gbk qstring line in.readli...

python 讀取檔案亂碼問題

一 問題 python讀取檔案時會遇到亂碼的問題 二 解決方法 1 已utf 8格式開啟文件 f open r e python liaotian.txt r encoding utf 8 f.seek 0,0 for each line in f print each line f.close 2...

python讀取檔案亂碼問題

今天突然想做乙個實時讀取 日誌檔案的程式,然後用python爬蟲直接爬取檔案,用唯讀的方式開啟避免對 系統 寫入日誌 造成影響。上 usr bin env python coding utf 8 import subprocess import time import chardet p 0 wit...