Note that there are some explanatory texts on larger screens.

plurals
  1. POSTL and UTF-8 file input/output. How to do it?
    text
    copied!<p>I use <code>wchar_t</code> for internal strings and UTF-8 for storage in files. I need to use STL to input/output text to <em>screen</em> and also do it by using full Lithuanian charset.<br> It's all fine because I'm not forced to do the same for <em>files</em>, so the following example does the job just fine:<pre><code>#include &lt;io.h&gt; <code>#</code>include &lt;fcntl.h&gt; <code>#</code>include &lt;iostream&gt; _setmode (_fileno(stdout), _O_U16TEXT); wcout &lt;&lt; L"AaĄąfl" &lt;&lt; endl; </code></pre>But I became curious and attempted to do the same with files with no success. Of course I could use formatted input/output, but that is... <em>discouraged</em>.<pre><code> FILE* fp; _wfopen_s (&amp;fp, L"utf-8_out_test.txt", L"w"); _setmode (_fileno (fp), _O_U8TEXT); _fwprintf_p (fp, L"AaĄą\nfl"); fclose (fp); _wfopen_s (&amp;fp, L"utf-8_in_test.txt", L"r"); _setmode (_fileno (fp), _O_U8TEXT); wchar_t text[256]; fseek (fp, NULL, SEEK_SET); fwscanf (fp, L"%s", text); wcout &lt;&lt; text &lt;&lt; endl; fwscanf (fp, L"%s", text); wcout &lt;&lt; text &lt;&lt; endl; fclose (fp);</code></pre>This snippet works <strong>perfectly</strong> (although I am not sure how it handles malformed chars). So, is there any way to:</p> <ul> <li>get <code>FILE*</code> or integer file handle form a <code>std::basic_*fstream</code>?</li> <li>simulate <code>_setmode ()</code> on it?</li> <li>extend <code>std::basic_*fstream</code> so it handles UTF-8 I/O?</li> </ul> <p>Yes, I am studying at an university and this is somewhat related to my assignments, but I am trying to figure this out for myself. It won't influence my grade or anything like that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload