search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

VC++(MFC) 繁中/簡中字串 (CString) 檔案寫入/讀取 不亂碼 [2018/08/21在VC2015測試過] – jashliao部落格

VC++(MFC) 繁中/簡中字串 (CString) 檔案寫入/讀取 不亂碼 [2018/08/21在VC2015測試過]


資料來源: http://aigo.iteye.com/blog/2299351

https://blog.csdn.net/Augusdi/article/details/8961008


[ MFC使用CFile读写Unicode字符集文件(文件头缺失导致乱码)/ MFC使用CFile读写Unicode字符集文件]


寫入:

#include   
  
#ifndef _UNICODE  
#define _UNICODE            //使用UNICODE编码  
#endif  
  
#include            //为了使用CString类  
  
const int UNICODE_TXT_FLG = 0xFEFF;  //UNICODE文本标示  
  
int main()  
{  
    FILE* WriteF;  
      
  
    CString Wstr = _T("一个测试写入文本");  
    WriteF = fopen("test.txt","w+");  
  
    if(WriteF)  
    {  
        fwrite(&UNICODE_TXT_FLG,2,1,WriteF);  //写入头部  
        fwrite(Wstr.GetBuffer(Wstr.GetLength()),Wstr.GetLength() * 2,1,WriteF);  
        fclose(WriteF);  
    }         
     
    return 0;  
}


讀取:

CFile ReadF(L"test.txt", CFile::modeRead);
TCHAR* temp = new TCHAR[ReadF.GetLength() / 2 + 1];
ReadF.Read(temp, ReadF.GetLength());
temp[ReadF.GetLength() / 2] = 0;
ReadF.Close();
strVal = temp;  



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦