在MFC中用CStdioFile对象输出utf-8和ansi编码的文本
来源:互联网 发布:java获取项目文件路径 编辑:程序博客网 时间:2024/05/18 03:24
转自: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 0
- 在MFC中用CStdioFile对象输出utf-8和ansi编码的文本
- [经验总结]利用MFC的CFile对象输出utf-8和ansi编码的文本
- ANSI和UTF-8文本编码
- ANSI和UTF-8文本编码的体积比较
- Ansi编码和UTF-8编码
- ANSI和UTF-8编码方式
- ANSI、ASCII、Unicode和UTF-8编码
- ANSI、ASCII、Unicode和UTF-8编码
- ANSI、ASCII、Unicode和UTF-8编码
- 用java实现文本文档的ANSI编码转化为UTF-8
- 将电脑新建文本文档txt的默认编码从ANSI改为utf-8
- MFC的CStdioFile输出和读取汉字乱码
- UTF-8转化ANSI编码
- .java文件的ANSI编码转换为UTF-8编码
- .java文件的ANSI编码转换为UTF-8编码
- URL中文中用的utf-8编码?
- utf-8和ANSI
- fstream类读取UTF-8、Unicode和ANSI文本文档乱码问题的解决方案
- Spring MVC防止数据重复提交
- 上证50指数成分股列表
- window下配置sublime和latex
- 音视频开发技术 单例模式
- 在发送请求时 java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=n
- 在MFC中用CStdioFile对象输出utf-8和ansi编码的文本
- 我常用的 15 个 Sketch Plugins
- 第三部分 管理篇 第九章 MongoDB shell之eval、进程
- What is fast-forward in git
- platform_driver_probe与platform_driver_register的区别
- Creating a Roulette Wheel Using HTML5 Canvas
- 策略模式
- iOS安全攻防(三)用iOSOpenDev开发NotificationCenter
- ViewDragHelper的使用