Unicode Strings in ini-Files
Write an Unicode String to an ini-File using WritePrivateProfileString
If you try to write an unicode string like e.g. "日本人"
to an ini-file then you will notice that the function WritePrivateProfileString
fails.
The solution is to create the ini-file with the encoding
UTF-16LE before writing an unicode string to it.
Please note: You need to create the file before
writing to it the first time (only once).
Sample Sourcecode:
CString inifile=L"c:\\temp\sample.ini";
FILE
*stream = NULL;
// make sure the file does
not already exist
if (_wfopen_s (&stream,inifile,L"r") ==
ENOENT)
{
// create
file with encoding UTF-16LE
if (_wfopen_s (&stream,inifile,L"w,
ccs=UTF-16LE") == 0)
{
// optional: add some text e.g.
fputws (L"// UNICODE INI FILE\n",stream);
}
}
if (stream != NULL) fclose (stream);
// write unicode string to ini-file
WritePrivateProfileString(L"SAMPLEKEY",L"SAMPLEVALUE",L"日本人",inifile);