How locale codeset impact the C++ program

Viewed 37

For the following code:

#include <locale>
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char **argv)
{
    locale loc("zh_CN.GBK");
    std::basic_ofstream<char32_t> output("output.txt");
    output.imbue(loc);
    char32_t w = U'\u6211';
    output << w;
}

What I want is let the ofstream dump a character with GBK code. However, after running the code in my computer, I found that it output the character's UTF-8 code. So I have 2 questions about that:

  1. What's the usage of the codeset in the locale?
  2. How should I modify the above code to dump the character with GBK characters?

Thanks a lot!

0 Answers
Related