欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用ofstream输出unicode

程序员文章站 2022-07-05 16:06:34
void saveWideFileHead(std::ofstream& out)// 写入文件内容前,先写入BOM { char const* const utf16head = "\xFF\xFE"; out.write(utf16head, 2); } void saveWideFileCon... ......
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();
}