在MFC中用CStdioFile对象输出utf-8和ansi编码的文本

转自:http://blog.csdn.net/thinkhy/article/details/5740748

下面的代码分别输出UTF-8和ANSI(简体中文Windows上是GBK编码)两种编码的文本文件。

//
// 写UTF-8文本

// 下列文件类操作中可能抛出一个CFileException异常

try {
        CStdioFile hfile( strFilePath, CFile::modeReadWrite);
                           
        if(hfile.m_hFile)
        {
                            DWORD dwFileLen = hfile.GetLength();
                           
                            if (0 == dwFileLen) // 文件为空时写入UTF字节序标记
                            {
                                     const unsigned char LeadBytes[]  = {0xEF, 0xBB, 0xBF};
                                     hfile.Write(LeadBytes, sizeof(LeadBytes));
                            }
 
            int nSrcLen = (int)wcslen(lpstrWord);
 
            CStringA utf8String(lpstrWord);
            int nBufLen = (nSrcLen+1) * 6;
            LPSTR buffer = utf8String.GetBufferSetLength(nBufLen);
 
            // 将UNICODE 转换成UTF8
            // 需要函数AtlUnicodeToUTF8 头文件: <atlenc.h>
            int nLen = AtlUnicodeToUTF8(lpstrWord, nSrcLen, buffer, nBufLen);             // int nLen = utf8String.GetLength();
 
            buffer[nLen] = 0;
            utf8String.ReleaseBuffer();
 
            //写文件
                            hfile.SeekToEnd();
            hfile.Write((LPCSTR)utf8String, nLen);
            hfile.Write("/r/n", 2);
 
            hfile.Close();
        }
    }
    catch(CFileException* pException)
    {
        CString strMsg;
                  
        TCHAR  szErrorMessage[512];
        if (pException->GetErrorMessage(szErrorMessage,
                            sizeof(szErrorMessage)/sizeof(*szErrorMessage), 0))
                strMsg.Format(_T("(%s:%d)/n%s"), _T(__FILE__), __LINE__, szErrorMessage);
            else
                strMsg.Format(_T("(%s:%d)"),  _T(__FILE__), __LINE__);
 
        AfxMessageBox(strMsg);
    }

/
/
// 写ANSI文本:

try {
        CStdioFile hfile( strHeiMaiUsrLibPath, CFile::modeReadWrite);
                           
        if(hfile.m_hFile)
        {
            // 将Unicode字符串赋给Ansi格式的CStringA,实现Unicode=>Ansi转换
            CStringA utf8String(lpstrWord);
            int nLen = utf8String.GetLength();
 
            // 写文件
            hfile.SeekToEnd();
            hfile.Write((LPCSTR)utf8String, nLen);
            hfile.Write("/r/n", 2);
 
            hfile.Close();
        }
    }
    catch(CFileException* pException)
    {
        CString strMsg;
                  
        TCHAR  szErrorMessage[512];
        if (pException->GetErrorMessage(szErrorMessage, sizeof(szErrorMessage)/sizeof(*szErrorMessage), 0))
                strMsg.Format(_T("(%s:%d)/n%s"), _T(__FILE__), __LINE__, szErrorMessage);
            else
                strMsg.Format(_T("(%s:%d)"),  _T(__FILE__), __LINE__);
 
        AfxMessageBox(strMsg);
    }


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值