Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set file encoding format to UTF8 in C++
    text
    copied!<p>A requirement for my software is that the encoding of a file which contains exported data shall be UTF8. But when I write the data to the file the encoding is always ANSI. (I use Notepad++ to check this.)</p> <p>What I'm currently doing is trying to convert the file manually by reading it, converting it to UTF8 and writing the text to a new file.</p> <p><code>line</code> is a <code>std::string</code><br> <code>inputFile</code> is an <code>std::ifstream</code><br> <code>pOutputFile</code> is a <code>FILE*</code> </p> <pre><code>// ... if( inputFile.is_open() ) { while( inputFile.good() ) { getline(inputFile,line); //1 DWORD dwCount = MultiByteToWideChar( CP_ACP, 0, line.c_str(), -1, NULL, 0 ); wchar_t *pwcharText; pwcharText = new wchar_t[ dwCount]; //2 MultiByteToWideChar( CP_ACP, 0, line.c_str(), -1, pwcharText, dwCount ); //3 dwCount = WideCharToMultiByte( CP_UTF8, 0, pwcharText, -1, NULL, 0, NULL, NULL ); char *pText; pText = new char[ dwCount ]; //4 WideCharToMultiByte( CP_UTF8, 0, pwcharText, -1, pText, dwCount, NULL, NULL ); fprintf(pOutputFile,pText); fprintf(pOutputFile,"\n"); delete[] pwcharText; delete[] pText; } } // ... </code></pre> <p>Unfortunately the encoding is still ANSI. I searched a while for a solution but I always encounter the solution via MultiByteToWideChar and WideCharToMultiByte. However, this doesn't seem to work. What am I missing here?</p> <p>I also looked here on SO for a solution but most UTF8 questions deal with C# and php stuff.</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