title: Detect charset of file dynamically in c++
body: <p>I am trying to read a file which may have any charset/codePage, but I don't which locale to set in order to read the file correctly.</p>
  
  <p>Below is my code snippet in which I am trying to read a file having charset as windows-1256, but I want to get the charset dynamically from the file being read so that I can set the locale accordingly.</p>
  
  <pre><code>std::wifstream input{ filename.c_str() };
  std::wstring content{ std::istreambuf_iterator&lt;wchar_t&gt;(input1), std::istreambuf_iterator&lt;wchar_t&gt;() };
  input.imbue(std::locale(".1256"));
  contents = ws2s(content); // Convert wstring to CString
  </code></pre>