MapViewOfFile和unicode [英] MapViewOfFile and unicode

查看:127
本文介绍了MapViewOfFile和unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个文件映射并修改我的文件,但是当我修改我的文件时,却不是我想要的,我的英语很差,您可以看到此代码
我的项目是unicode

i want create a filemapping and modify my file,but when i modify my file ,but it not what i want,my english is very poor ,you can see this code
my project is unicode

<br />
hfile=::CreateFile(TEXT("c:\\1.txt"),GENERIC_ALL|FILE_ALL_ACCESS,FILE_SHARE_WRITE|\<br />
FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);<br />
HANDLE hmap=::CreateFileMapping(hfile,NULL,PAGE_READWRITE,0,0,NULL);<br />
TCHAR*pmap=(TCHAR*)::MapViewOfFile(hmap,FILE_MAP_ALL_ACCESS,0,0,NULL);<br />
TCHAR*tmp=pmap;<br />
lstrcpy(tmp,TEXT("my test"));<br />
::UnmapViewOfFile(pmap);<br />
::CloseHandle(hmap);<br />
::CloseHandle(hfile);<br />


c:\ 1.txt为我的测试",我希望它为我的测试"
我该如何实现这个


c:\1.txt is "m y t e s t" ,i want it is "my test"
how do i achieve this

推荐答案

如果要创建 UNICODE 文本文件,则应添加 BOM 文件开头的(字节顺序标记)"指示操作系统有关文件编码的信息.如果不这样做,文本文件将始终以 ASCII 的形式写入磁盘;您的代码是否编译为使用 UNICODE 都没有关系.

换句话说,第一次创建文本文件之前,应先对其进行任何操作,然后在其中写入两个二进制字节: 0xFF 0xFE .您可以通过以下方式进行操作:

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);



有关 BOM 的更多信息,请单击以下链接: http://www. websina.com/bugzero/kb/unicode-bom.html [ ^ ]



For more informations about BOM you can follow this link: http://www.websina.com/bugzero/kb/unicode-bom.html[^]


这篇关于MapViewOfFile和unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆