title: How to read UTF-8 encoded text file using std::ifstream?
body: <p>I'm having a hard time to parse an xml file.</p>
  
  <p>The file was saved with UTF-8 Encoding. </p>
  
  <p>Normal ASCII are read correctly, but Korean characters are not.</p>
  
  <p>So I made a simple program to read a UTF-8 text file and print the content.</p>
  
  <h2>Text File(test.txt)</h2>
  
  <pre><code>ABC가나다
  </code></pre>
  
  <h2>Test Program</h2>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;iterator&gt;
  #include &lt;streambuf&gt;
  
  const char* hex(char c) {
      const char REF[] = "0123456789ABCDEF";
      static char output[3] = "XX";
      output[0] = REF[0x0f &amp; c&gt;&gt;4];
      output[1] = REF[0x0f &amp; c];
      return output;
  }
  
  int main() {
      std::cout &lt;&lt; "File(ifstream) : ";
      std::ifstream file("test.txt");
      std::string buffer((std::istreambuf_iterator&lt;char&gt;(file)), std::istreambuf_iterator&lt;char&gt;());
      for (auto c : buffer) {
          std::cout &lt;&lt; hex(c)&lt;&lt; " ";
      }
      std::cout &lt;&lt; std::endl;
      std::cout &lt;&lt; buffer &lt;&lt; std::endl;
  
      //String literal
      std::string str = "ABC가나다";
      std::cout &lt;&lt; "String literal : ";
      for (auto c : str) {
          std::cout &lt;&lt; hex(c) &lt;&lt; " ";
      }
      std::cout &lt;&lt; std::endl;
      std::cout &lt;&lt; str &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <h2>Output</h2>
  
  <pre><code>File(ifstream) : 41 42 43 EA B0 80 EB 82 98 EB 8B A4
  ABC媛?섎떎
  String literal : 41 42 43 B0 A1 B3 AA B4 D9
  ABC가나다
  </code></pre>
  
  <p>The output said that characters are encoded differently in string literal and file.</p>
  
  <p>So far as I know, in c++ <code>char</code> strings are encoded in UTF-8 so we can see them through <code>printf</code> or <code>cout</code>. So their bytes were supposed to be same, but they were different actually...</p>
  
  <p><strong>Is there any way to read UTF-8 text file using <code>std::ifstream</code>?</strong></p>
  
  <hr>
  
  <p>I succeed to parse xml file using <code>std::wifstream</code> following <a href="https://sockbandit.wordpress.com/2012/05/31/c-read-and-write-utf-8-file-using-standard-libarary/" rel="noreferrer">this article</a>.</p>
  
  <p>But most of the libraries I'm using are supporting only <code>const char*</code> string so I'm searching for another way to use <code>std::ifstream</code>.</p>
  
  <p>And also I've read <a href="https://stackoverflow.com/a/17871934/3964522">this article</a> saying that do not use <code>wchar_t</code>. Treating <code>char</code> string as multi-bytes character is sufficient.</p>