先看程序:
#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;
}
再調試,不是有“中文”了?