C++ Reading UTF-8 file OK, converting to UTF-16 OK, writing back to file
NOT OK
I'm using the following code to read from a text (.xml) file i have
created in a text editor (Notepad++), convert the UTF-8 text i read from
it into UTF-16 so that windows API functions can use it, and then write
this UTF-16 encoded text back into a second file.
My problem is that when i open the output file in Notepad++, i don't get
what i'm expecting to see, not matter what encoding i ask the text editor
to use. There are null characters before almost every character in the
file. I assume that either i did something wrong while writting the UTF-16
to the output file or Notepad++ is reading as single byte chars.
Any idea please ? Here is the code :
#define UNICODE
// includes...
int main( int argc, char * argv[] )
{
FILE * pzInFile,
* pzOutFile;
try
{
char sUtf8[8192];
char * pcDst = sUtf8;
wchar_t wsUtf16[8192];
_wfopen_s( & pzInFile, L"../config-sample.xml", L"r" );
_wfopen_s( & pzOutFile, L"../config-sample2.xml", L"w+" );
if( pzInFile && pzOutFile )
{
size_t uiRead;
while( uiRead = fread_s( pcDst, sizeof( sUtf8 ), 1, 1,
pzInFile ) )
{
pcDst += uiRead;
}
size_t uiLen = pcDst - sUtf8;
sUtf8[uiLen] = 0;
MultiByteToWideChar( CP_UTF8, 0, sUtf8, 8192, wsUtf16, 8192 );
// UTF-8 to UTF-16
fwrite( wsUtf16, 1, uiLen, pzOutFile );
}
else
{
throw L"Failed to open file";
}
}
catch( const wchar_t * pwsMsg )
{
::MessageBox( NULL, pwsMsg, L"Error", MB_OK | MB_TOPMOST |
MB_SETFOREGROUND );
}
if( pzInFile )
{
fclose( pzInFile );
pzInFile = 0;
}
if( pzOutFile )
{
fclose( pzOutFile );
pzOutFile = 0;
}
return 0;
}
Thanks for your help !
No comments:
Post a Comment