#include <atlconv.h> // 引入对转换宏的支持
CStringW strChinese = L"设置当前结构体的对齐方式为1字节";
// 将Unicode CStringW转换为UTF-8编码的std::string
std::string utf8String;
CStringA utf8Encoded;
USES_CONVERSION;
utf8Encoded = CW2A(strChinese.GetString(), CP_UTF8);
utf8String = utf8Encoded.GetString();
// 打开一个CStdioFile实例以写入UTF-8数据
CStdioFile utf8File;
if (utf8File.Open(_T("output_utf8.txt"), CFile::modeCreate | CFile::modeWrite))
{
// 写入UTF-8编码的字符串
utf8File.Write(utf8Encoded.GetString(), utf8Encoded.GetLength());
// 关闭文件
utf8File.Close();
}
else
{
// 打开文件失败处理...
}
Unicode CString 写入中文的方法 此方法可行,已经测试。