看前面黑咚咚,待俺上前杀它个干干净净。。。

 

wchar_t 宽字节流写入中文的问题

先看程序:
#include "stdafx.h"
#include <fstream>
int _tmain(int argc, _TCHAR* argv[])
{
     std::wofstream   test(L"Test.Log");
     test << L"hello 中文";

     return 0;
}

UNICODE编译、调试;结果中Test.Log文件的内容只有“hell”没有“中文”。

这是因为C++标准库的国际化设计问题,你需要设置locale。

#include "stdafx.h"

#include <fstream>
#include <locale>

int _tmain(int argc, _TCHAR* argv[])
{
 std::wofstream   test(L"Test.Log");

 test.imbue( std::locale("CHS") );

 test << L"hello 中文";

 return 0;
}

再调试,不是有“中文”了?

posted on 2010-05-23 13:15 山城,山 阅读(912) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

统计

常用链接

留言簿

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜