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 山城,山 閱讀(965) 評論(0) 編輯 收藏 引用 所屬分類: C++