天天看點

QT 傳輸圖檔出現資料缺失

最近在學習QT的程式設計,用QT寫一個TFTP的用戶端,在傳輸檔案時出現了資料缺失。通wireshark抓包發現資料包并沒有錯誤,但是我傳圖檔卻出現了錯誤。

QT 傳輸圖檔出現資料缺失
QT 傳輸圖檔出現資料缺失

代碼如下:

QFile pFile = new QFile(pFilename);
if(!pFile->open(QIODevice::ReadWrite | QIODevice::Text))
return;
           

因為在設定檔案的讀寫時設定了QIODevice::Text,而查詢QT文檔可以發現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.

當讀取時,行結束終止符被轉換為“\ n”。 當寫入時,行結束終止符被轉換為本地編碼,例如Win32的’\ r \ n’。

在讀寫圖檔時将圖檔中的結束終止符進行了轉換,導緻圖檔出錯。

隻需将QIODevice::Tex參數去掉即可

QFile pFile = new QFile(pFilename);
if(!pFile->open(QIODevice::ReadWrite))
return;
           

繼續閱讀