Zi 字媒體
2017-07-25T20:27:27+00:00
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;
寫了
5860316篇文章,獲得
23313次喜歡