参考
MFC中CStdioFile处理文本文件乱码的原因及解决方法(编程 Unicode 环境下读写 ANSI 文件)http://hi.baidu.com/jfc_09/blog/item/f4d7fdd72685a8cf50da4b79.html
VC 编程ANSI环境下读写Unicode文件
http://blog.csdn.net/sunboy_2050/archive/2009/12/17/5019900.aspx-->
CStdioFile在UNICODE环境下读取文本行〔转〕 - 乖小猫的日志 - 网易博
!!!http://blog.163.com/neverforget_yang/blog/static/13095275720104482459666/
BOOL CLanguageManager::ParseResourceFile(LPCTSTR lpszFilePath, LPCTSTR lpszHeaderFilePath)
{.. // resource file
CStdioFile Resfile;
if (Resfile.Open(lpszFilePath, CFile::modeRead))
{
int index =0;
CString src;
CString strHeaderRow;
while (Resfile.ReadString(strHeaderRow))
{
!!!Ansi2Unicode(strHeaderRow);
src = src + _T("\r\n");
src = src + strHeaderRow;
}
// 将Unicode字符转换为Char型字符
#define BUFFER_SIZE_KILO 0
int UnicodeToChar(CString &strIn, char *pchOut, int nCharLen)
{
if(pchOut == NULL)
{
return 0;
}
int nLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)strIn.GetBuffer(BUFFER_SIZE_KILO),-1, NULL, 0, NULL, NULL);
nLen = min(nLen, nCharLen);
WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)strIn.GetBuffer(BUFFER_SIZE_KILO), -1, pchOut,
nLen, NULL, NULL);
if(nLen < nCharLen)
{
pchOut[nLen] = 0;
}
return nLen;
}
void Ansi2Unicode(CString &str)
{
//注:此函数在编译的时候会提示
// warning C4244: '=' : conversion from 'unsigned short' to 'char', possible loss of data
// 不用管它,丢失的数据是我们不需要的。
char *szBuf = new char[str.GetLength()];
for (int i = 0 ; i < str.GetLength(); i++)
{
szBuf[i] = str.GetAt(i);
}
CharToUnicode(szBuf , &str);
delete []szBuf;
CStdioFile Resfile; if (Resfile.Open(lpszFilePath, CFile::modeRead)) { int index =0; CString src; CString str_Unicode,str_MSBC; while (Resfile.ReadString(str_MSBC)) { // 注意:采用这种方式处理的文件必须为ANSI编码,不能使UNICODE // 把“_MSBC环境”下的“CString”(CHAR),直接填充到了“Unicode环境”下的“CString”(WCHAR)中, // 这样会导致“汉字”显示成乱码。如果想正确显示,需要对该字符串进行相应的处理。 if(str_MSBC != _T("")) { // 将保存成“宽字节类型”的“单字节类型”字符串(Unicode环境), // 还原为正确的“单字节类型”字符串(_MSBC环境)。 _UnicodeOf_MSBCTo_MSBC(&str_MSBC); // 将“单字节的字串符类型(_MSBC环境)”转换为“宽字节的字符串类型(Unicode环境)” _MSBCTo_Unicode(str_MSBC, &str_Unicode);
}