我正在尝试将 unicode 字符写入文件,std::wofstream
但put
orwrite
函数不写入任何字符。
示例代码:
#include <fstream>
#include <iostream>
int main()
{
std::wofstream file;
file.open("output.txt", std::ios::app);
if (file.is_open())
{
wchar_t test = L'й';
const wchar_t* str = L"фывдлао";
file.put(test);
file.write(str, sizeof(str));
file.close();
}
else
{
std::wcerr << L"Failed to open file" << std::endl;
}
std::cin.get();
return 0;
}
output.txt
文件为空,执行代码后没有写入 wchar/string,为什么?我究竟做错了什么?
编辑: 更正的代码:
#include <fstream>
#include <iostream>
int main()
{
std::wofstream file;
file.open("output.txt", std::ios::app);
if (file.is_open())
{
wchar_t test = L'й';
const wchar_t* str = L"фывдлао";
file.put(test);
if (!file.good())
{
std::wcerr << L"Failed to write" << std::endl;
}
file.write(str, 8);
file.close();
}
else
{
std::wcerr << L"Failed to open file" << std::endl;
}
std::cin.get();
return 0;
}
应用代码更正后,我看到了,Failed to write
但我仍然不明白我需要做什么来编写宽字符串和字符?