#include <tchar.h>
#include <string>
#include <fstream>
std::string ToUTF8(const wchar_t* buffer, int len)
{
int size = ::WideCharToMultiByte(CP_UTF8, 0, buffer, len, NULL, 0, NULL, NULL);
if (size == 0)
return "";
std::string newbuffer;
newbuffer.resize(size);
::WideCharToMultiByte(CP_UTF8, 0, buffer, len,
const_cast<char*>(newbuffer.c_str()), size, NULL, NULL);
return newbuffer;
}
std::string ToUTF8(const std::wstring& str)
{
return ToUTF8(str.c_str(), (int) str.size());
}
int _tmain(int argc, _TCHAR* argv[])
{
std::ofstream test_file;
test_file.open("d:\\test.txt", std::ios::out | std::ios::binary);
std::wstring text =
L"中文字符";
std::string outtext = ToUTF8(text);
test_file << outtext;
test_file.close();
return 0;
}
c++ 创建Utf8 文件
最新推荐文章于 2024-09-01 07:59:44 发布