前提:VS2010 ,MFC ,文本文件为ANSI格式。

读文件:

CString str,fileContent;
CStdioFile myFile, File;
if(myFile.Open(GeneralUtils::GetModuleDir()+_T(\\mx.txt), CFile::modeRead))
{
 //读取
 while(myFile.ReadString(str))
 {
  str = str + _T("\n");
  ReadStringCharToUnicode(str);
  fileContent = fileContent + str;
 }
 myFile.Close();
}

写文件:

*.h

#include <locale.h>

*.cpp

setlocale(LC_CTYPE,"chs");
myFile.Open(GeneralUtils::GetModuleDir()+_T(\\mx2.txt), CFile::modeWrite | CFile::modeCreate); 
myFile.WriteString(fileContent);
myFile.Close();

void ReadStringCharToUnicode(CString &str)
{
 char *szBuf = new char[str.GetLength() + 1];//注意“+1”,char字符要求结束符,而CString没有
 memset(szBuf, '\0',str.GetLength());

int i;
 for ( i = 0 ; i < str.GetLength(); i++)
 {
  szBuf[i] = (char)str.GetAt(i);
 }
 szBuf[i] = '\0';//结束符。否则会在末尾产生乱码。

int nLen;
 WCHAR *ptch;
 CString strOut;
 if(szBuf == NULL)
 {
  return ;
 }
 nLen = MultiByteToWideChar(CP_ACP, 0, szBuf, -1, NULL, 0);//获得需要的宽字符字节数
 ptch = new WCHAR[nLen];
 memset(ptch, '\0', nLen);
 MultiByteToWideChar(CP_ACP, 0, szBuf, -1, ptch, nLen);
 str.Format(_T("%s"), ptch);

if(NULL != ptch)
  delete [] ptch;
 ptch = NULL;

if(NULL != szBuf)
  delete []szBuf;
 szBuf = NULL;
 return ;
}

最新文章

  1. Node.js:OS模块
  2. XtraBackup出现 Can't connect to local MySQL server through socket '/tmp/mysql.sock'
  3. 【Java每日一题】20161020
  4. Meet Python: little notes
  5. linux shell 字符串操作详解 (长度,读取,替换,截取,连接,对比,删除,位置 )
  6. Git 10 周年之际,创始人 Linus Torvalds 访谈
  7. [Linux]常用命令与目录全拼
  8. FileInputStream 与 BufferedInputStream 效率对比
  9. careercup-链表 2.3
  10. json数据渲染表单元素出现的问题
  11. Python 安装包报错
  12. 第七周博客作业 &lt;西北师范大学| 周安伟&gt;
  13. Arrays.asList() 的使用注意
  14. Numpy 数组简单操作
  15. IOS 缓存方案(按需缓存 、 预缓存)及 低网速模拟
  16. 并发队列ConcurrentLinkedQueue、阻塞队列AraayBlockingQueue、阻塞队列LinkedBlockingQueue 区别和使用场景总结
  17. Collabtive 系统 SQL 注入实验(补充)
  18. kubeadm高可用master节点部署文档
  19. 【剑指offer】第一个只出现一次的字符
  20. 9012年,我终于找到了Pypi稳定的源....

热门文章

  1. Jquery hover 事件
  2. 转:[python] pip 升级所有的包
  3. C# 给图片添加透明的文字、图片水印
  4. C# 抽象(2)
  5. Spring Boot多数据源
  6. ubuntu17安装以及相关问题的解决
  7. 老男孩Day10作业:主机管理程序
  8. HTML5+CSS实现三列布局自适应
  9. screen虚拟终端
  10. PHP 实时生成并下载超大数据量的 Excel 文件