網上流行的答案顯然沒有命中問題的要害!其實,unicode字符已經被Cfile忠實地輸入了文件當中!遺憾的是字處理軟件(如:記事本)不知道其中是unicode字符,將他們統統當做了單字節字符處理。
要想讓字處理軟件識別unicode必須在文件頭上加入unicode編碼的前導字符:0xff, 0xfe。
於是,可以用下面的代碼實現漢字的輸出且用字處理軟件打開不會亂碼
CFile DPDA;
CString FileName;
CString strLine;
FileName=_T("C:\\DPDA.txt");
DPDA.Open(FileName,CFile::modeCreate|CFile::modeWrite);
strLine="張三、李四";
WORD unicode = 0xFEFF; //這句重要
DPDA.Write(&unicode,2); //這句重要
DPDA.Write(strLine,wcslen(strLine)*sizeof(wchar_t)); //這句重要
CString strEnter;
strEnter="\r\n";
DPDA.Write(strEnter,4);
DPDA.Write(strLine,wcslen(strLine)*sizeof(wchar_t));
DPDA.Close();
此文屬於轉帖,但是由於整理的時間過長了 忘記原來的出處,在此向原作者致敬,也希望獲得原作者的原諒
未註明轉載出處