If you want to create
UNICODE text files, you should add a
BOM (byte order mark) at the begin of the file to instruct the operating system about the file encoding. If you don't do it, text files are always written on disk as
ASCII; it doesn't matter if your code is compiled to use
UNICODE.
In other words, the first time you create the text file, prior to any operation on it, you shouyld write two binary bytes on it:
0xFF and
0xFE. You can do it in the following way:
hfile = ::CreateFile(
TEXT("c:\\1.txt"),
GENERIC_ALL | FILE_ALL_ACCESS,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
HANDLE hmap = ::CreateFileMapping(
hfile,
NULL,
PAGE_READWRITE,
0,
0,
NULL);
TCHAR *pmap = (TCHAR*)::MapViewOfFile(
hmap,
FILE_MAP_ALL_ACCESS,
0,
0,
NULL);
*pmap = (TCHAR)0xFEFF;
TCHAR *tmp = pmap + 1;
lstrcpy(tmp, TEXT("my test"));
::UnmapViewOfFile(pmap);
::CloseHandle(hmap);
::CloseHandle(hfile);
For more informations about
BOM you can follow this link:
http://www.websina.com/bugzero/kb/unicode-bom.html[
^]