Answer the question
In order to leave comments, you need to log in
How to correctly read a file in utf-8 encoding using getline in c++?
There is the following code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void main()
{
setlocale(LC_ALL, "utf-8");
wifstream file("Text.txt");
wofstream file1("Text1.txt");
wstring str;
while (getline(file,str))
{
wcout << str << endl;
file1 << str << endl;
}
system("pause");
}
dddd
qqqq
яяяя
中華人民共和國
я╗┐dddd
qqqq
╤П╤П╤П╤П
ф╕ншПпф║║ц░СхЕ▒хТМхЬЛ
Answer the question
In order to leave comments, you need to log in
Try this code to read from a file
#include <sstream>
#include <fstream>
#include <codecvt>
std::wstring readFile(const char* filename)
{
std::wifstream wif(filename);
wif.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
std::wstringstream wss;
wss << wif.rdbuf();
return wss.str();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question