Записать wstring в wofstream или преобразование wstring в массив байтов
Выдаёт код code=3221226505
при запуске.
В файл ничего не записывается.
#include <iostream>
#include <string>
#include <fstream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main () {
setmode(_fileno(stdout), _O_U16TEXT);
setmode(_fileno(stdin), _O_U16TEXT);
setmode(_fileno(stderr), _O_U16TEXT);
wofstream file;
file.open(L"Test.txt");
wstring wstr = L"Кириллица";
try {
file << wstr;
} catch (exception &e) {
wcout << e.what() << endl;
}
file.close();
return 0;
}
Возможен второй вариант - перевод wstring в массив байтов (char) и последующая запись этих байтов в файл:
Такой код работает корректно:
#include <iostream>
#include <string>
#include <fstream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main () {
setmode(_fileno(stdout), _O_U16TEXT);
setmode(_fileno(stdin), _O_U16TEXT);
setmode(_fileno(stderr), _O_U16TEXT);
ofstream file;
ifstream in; // UTF-8 файл !!!
in.open("Test_in.txt", ios::binary);
file.open("Test.txt", ios::binary);
char buf;
char &buffer = buf;
try {
while (in.get(buffer)) file << buf;
} catch (exception &e) {
wcout << e.what() << endl;
}
file.close();
return 0;
}
Но проблема в том, что wstring я получаю не из файла. Кто-то знает способ перевода wstring в массив char?