I'm trying to write unicode characters to file with std::wofstream
but the put
or write
function doesn't write any characters.
Sample code:
#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
file is empty, no wchar/string is written after executing code, why? what am I doing wrong?
EDIT: Corected code:
#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;
}
After applying code correction I'm presented with Failed to write
but I still don't understand what do I need to do to write wide strings and chars?