wchar_t 寬字節(jié)流寫入中文的問(wèn)題
先看程序:
#include "stdafx.h"
#include <fstream>
int _tmain(int argc, _TCHAR* argv[])
{
std::wofstream test(L"Test.Log");
test << L"hello 中文";
return 0;
}
UNICODE編譯、調(diào)試;結(jié)果中Test.Log文件的內(nèi)容只有“hell”沒(méi)有“中文”。
這是因?yàn)镃++標(biāo)準(zhǔn)庫(kù)的國(guó)際化設(shè)計(jì)問(wèn)題,你需要設(shè)置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;
}
再調(diào)試,不是有“中文”了?
posted on 2010-05-23 13:15 山城,山 閱讀(965) 評(píng)論(0) 編輯 收藏 引用 所屬分類: C++