title: UTF8 string to int
body: <p>I have been struggling for some time with trying to extract an int from a UTF8 file: </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  int main()
  {
      ifstream file("UTF8.txt");
      if(file.is_open())
      {
          string line;
          getline(file, line);
          istringstream ss(line);
          int a;
          ss &gt;&gt; a;
          if(ss.fail())
          {
              cout &lt;&lt; "Error parsing" &lt;&lt; endl;
              ss.clear();
          }
          getline(file, line);
          cout &lt;&lt; a &lt;&lt; endl &lt;&lt; line &lt;&lt; endl;
          file.close();
      }
  }
  </code></pre>
  
  <p>The file contains 2 lines: "42" and "è_é", and is saved in Notepad as UTF8. The above works when the file is ANSI, but fails when it is Unicode. I've tried a number of things, the most promising one being to set the locale, but I would like the program to be independent on the locale of the computer (i.e. read chinese characters even if the PC is a US one).
  Honestly, I'm out of ideas now. I'd like to avoid using CStrings from Qt if possible.</p>
  
  <h2>UPDATE</h2>
  
  <p>The following displays "0", "Error parsing" because of one weird character at the very beginning of the file. An empty line, discarded when read, just before the number makes it work but I can't change the file in the final program. Accents are not displayed properly in the console, but when I write the output to a file all is well and that's all I need. So it's only that issue with the beginning of the file!</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;locale&gt;
  #include &lt;codecvt&gt;
  #include &lt;sstream&gt;
  
  int main()
  {
      std::ifstream file("UTF8.srt");
      file.imbue(std::locale(file.getloc(),
          new std::codecvt_utf8&lt;wchar_t,0x10ffff,std::consume_header&gt;));
      if (file.is_open()) {
          std::string line;
          std::getline(file,line);
          std::istringstream ss{line};
          int a;
          ss &gt;&gt; a;
          if (ss.fail()) {
              std::cout &lt;&lt; "Error parsing" &lt;&lt; std::endl;
              ss.clear();
          }
          getline(file,line);
          std::cout &lt;&lt; a &lt;&lt; std::endl &lt;&lt; line &lt;&lt; std::endl;
          file.close();
      }
  }
  </code></pre>
  
  <h2>SOLUTION</h2>
  
  <p>The following works, with the input file content as follows:</p>
  
  <pre><code>5
  bla bla é_è
  
  6
  truc è_é
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;cstdint&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  
  // Do not get used to it:
  // using namespace std;
  
  inline const char* skip_utf8_bom(const char* s, std::size_t size)
  {
      if(3 &lt;= size &amp;&amp; s[0] == char(0xEF) &amp;&amp; s[1] == char(0xBB) &amp;&amp; s[2] == char(0xBF))
          s += 3;
      return s;
  }
  
  int main()
  {
      std::ifstream file("UTF8.txt");
      std::ofstream fileO("UTF8_copy.txt");
      if(!file || !fileO) {
          std::cout &lt;&lt; "Error opening files" &lt;&lt; std::endl;
      }
      else {
          std::string line;
  
          //Parse the first number
          std::getline(file, line);
          {
              const char* linePtr = skip_utf8_bom(line.c_str(), line.size());
              std::istringstream input(linePtr);
              int a = -1;
              input &gt;&gt; a;
              if( ! input) {
                  std::cout &lt;&lt; "Error parsing" &lt;&lt; std::endl;
              }
              std::cout &lt;&lt; "Number 1: " &lt;&lt; a &lt;&lt; std::endl;
              fileO &lt;&lt; a &lt;&lt; std::endl;
          }
  
          //Copy the following line as is
          std::getline(file, line);
          fileO &lt;&lt; line &lt;&lt; std::endl;
  
          //Discard empty line, copy it in the output file
          std::getline(file, line);
          fileO &lt;&lt; std::endl;
  
          //Parse the second number
          std::getline(file, line);
          {
              const char* linePtr = skip_utf8_bom(line.c_str(), line.size());
              std::istringstream input(linePtr);
              int a = -1;
              input &gt;&gt; a;
              if( ! input) {
                  std::cout &lt;&lt; "Error parsing" &lt;&lt; std::endl;
              }
              std::cout &lt;&lt; "Number 1: " &lt;&lt; a &lt;&lt; std::endl;
              fileO &lt;&lt; a &lt;&lt; std::endl;
          }
  
          //Copy the following line as is
          std::getline(file, line);
          fileO &lt;&lt; line &lt;&lt; std::endl;
  
          file.close();
          fileO.close();
      }
  
      return 0;
  }
  </code></pre>