How to read an UTF-8 encoded file containing Chinese characters and output them correctly on console?

后端 未结 3 1696
生来不讨喜
生来不讨喜 2020-12-10 07:55

I am writing a web crawler to fetch some Chinese web files. The fetched files are encoded in utf-8. And I need to read those file to do some parse, such as extracting the UR

3条回答
  •  伪装坚强ぢ
    2020-12-10 08:59

    In general, use the w variants, (wstring, wfstream, wcout), set your locales to match the requirements, hang an L on the front of string literals. locale::global(locale("")) sets up to match the environment default, then on each stream that isn't running according to that default e.g. wcout.imbue(locale("Chinese_China.936")) might be Microsoft's name for your terminal's locale settings. This has always been enough to do what I want, hope it works as well for you.

    #include 
    #include 
    using namespace std;
    int main() {
      locale::global(locale(""));
      wstring word;
      while (wcin >>word)
        wcout<

提交回复
热议问题