使用ofstream输出unicode

void saveWideFileHead(std::ofstream& out)// 写入文件内容前,先写入BOM
{
	char const* const utf16head = "\xFF\xFE";
	out.write(utf16head, 2);
}

void saveWideFileContent(std::ofstream& out, wchar_t const* str, int size)
{
	char const* pos = (char const*)str;
	out.write(pos, size);
}

void saveWideFileCRLF(std::ofstream& out)// 写入回车换行符
{
	char const* const utf16head = "\x0D\x00\x0A\x00";
	out.write(utf16head, 4);
}

void Test()
{
	std::ofstream of("test.log", std::ios::binary | std::ios::out);
	saveWideFileHead(of);
	CString str("hello中国world1234");
	saveWideFileContent(of, str, str.GetLength() * 2);
	saveWideFileCRLF(of);
	saveWideFileContent(of, str, str.GetLength() * 2);
	of.close();
}

  

posted @ 2019-02-20 11:14  大鹏赋  阅读(1519)  评论(1)    收藏  举报