K
K
KorwinD2017-07-30 12:21:25
C++ / C#
KorwinD, 2017-07-30 12:21:25

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");
}

The Text.txt file contains the following lines:
dddd
qqqq
яяяя
中華人民共和國

The output to the console is:
я╗┐dddd
qqqq
╤П╤П╤П╤П
ф╕ншПпф║║ц░СхЕ▒хТМхЬЛ

But at the same time, the output to the Text1.txt file is absolutely correct, unlike the console output. Firstly, I would like to know how to make the correct output to the console, and secondly, where does "ya╗┐" come from in the first line.?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ananiev, 2017-07-30
@SaNNy32

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 question

Ask a Question

731 491 924 answers to any question