I have a JSON file with the following content (for example):
{
"excel_filepath": "excel_file.xlsx",
"line_length": 5.0,
"record_frequency": 2.5,
"report_file_name": "\u041f\u0421 \u041f\u0440\u043e\u043c\u0437\u043e\u043d\u0430 - \u041f\u0421 \u041f\u043e\u0433\u043e\u0440\u0435\u043b\u043e\u0432\u043e (\u0426.1)",
"line_type": 1,
}
This JSON file is generated by Python script.
For reading the JSON file, I use the <nlohmann/json.hpp>
library (I found it simple for my case):
using json = nlohmann::json;
std::ifstream f("temp_data.json");
json data = json::parse(f);
What I want to do is to read the "report_file_name"
value and create a simple .txt
file named as the value of the report_file_name
key, which is stored as Unicode, as you can see.
What I am trying to do is as follows:
_setmode(_fileno(stdout), _O_U16TEXT);
const locale utf8_locale = locale(locale(), new codecvt_utf8<wchar_t>());
string report_file_name = data["report_file_name"];
for (auto unicode_char : report_file_name)
{
wcout << typeid(unicode_char).name() << ": " << unicode_char << endl;
}
wofstream report_file(report_file_name + L".txt");
report_file.imbue(utf8_locale);
This gives an output as:
char: Ð
char:
char: Ð
char: ¡
char:
char: Ð
char:
char: Ñ
char:
char: Ð
char: ¾
... and so on
I have to note that I somehow managed to write Cyrillic letters into a report file. Interestingly, when I do:
wcout << L"\u041f\u0421" << endl;
It prints out Cyrillic letters (ПС
) correctly. Also, no problem with creating the report .txt
file with a Cyrillic name from code:
wofstream report_file(L"Отчет.txt"); // fine!
Am I doing something wrong? I'm using Windows 10, MVS 2022 with C++17 Standard. If this is helpful.