使用ofstream輸出unicode

from2019發表於2019-02-20
void saveWideFileHead(std::ofstream& out)// 寫入檔案內容前,先寫入BOM
{
	char const* const utf16head = "xFFxFE";
	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 = "x0Dx00x0Ax00";
	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();
}

  

相關文章