先看程序:
#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”沒有“中文”。
這是因?yàn)镃++標(biāo)準(zhǔn)庫的國(guó)際化設(shè)計(jì)問題,你需要設(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)試,不是有“中文”了?