解决方法:
包含头文件:#include <locale>
然后加入下列代码:
CStdioFile file;
if (!file.Open(strPath, CFile::modeCreate | CFile::modeReadWrite))
return FALSE;
char * old_locale = _strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, "chs");//设定
CString str = _T("哈哈哈");
file.WriteString(str);
setlocale(LC_ALL, old_locale);//还原
free(old_locale);
file.Close();
其中setlocale的第一个参数值应该用LC_ALL,使用其它值会写入乱码(亲测)。