CFile的Unicode宽字符写文件
来源:互联网 发布:淘宝电商平台技术架构 编辑:程序博客网 时间:2024/04/26 23:32
CFile的Unicode宽字符写文件
ANSI字符集称为窄字符,8位,C语言用unsigned char表示,不能存放世界上所有语言所有文字。
UNICODE字符集称为宽字符,16位,C语言用unsigned short表示,可以存放世界上所有语言所有文字。
宽字符字符串表示为一个wchar_t[]数组并由wchar_t* 指针指向它。可以通过用字母L作为字符的前缀将任何ASCII字符表示为宽字符形式。例如,L'/0' 是终止宽(16位)NULL字符。同样,可以通过用字母L作为ASCII字符串的前缀 (L"Hello") 将任何ASCII字符串表示为宽字符字符串形式。
ANSI的文件的CFile读写代码一般如下:
CFile ansifile;
CString str=_T(”Hello!”);
ansifile.Open(“文件名”,CFile::modeCreate|CFile::modeReadWrite);
ansifile.Write(str,str.Getlength());
ansifile.Close();
上面代码虽然使用宏_T()来申明了CString对象,但是在文件写入的时候,str.Getlength()的值任然位窄字符的长度;如果采用str.Getlength()*sizeof(wchar_t)的话,写入文件的将会出现乱码。因此CFile如果要写入宽字符的话,应该采用以下方式:
CFile unicodefile;
Wchar_t *str=L”Hello!”;
unicodefile.Open(“文件名”,CFile::modeCreate|CFile::modeReadWrite);
unicodefile.Write(str,wcslen(str)));
unicodefile.Close();
CString 和wchar_t*之间的转换:
1. CString 转 wchar_t*
CString str = "Hell Kesuer";
wchar_t *wstr=path.AllocSysString();
或者:
wchar_t wstr [256];
MultiByteToWideChar(CP_ACP,0,str,-1,wcstring,256);
或者:
USES_CONVERSION;
wchar_t* wstr=A2W(str);
2. wchar_t*转CString
wchar_t wcstring[256];
CString str ;;
WideCharToMultiByte(CP_ACP,0,wcstring,256,str.GetBuffer(0),256,NULL,NULL);
str.ReleaseBuffer(0);
MultiByteToWideChar
函数功能:该函数映射一个字符串到一个宽字符(unicode)的字符串。由该函数映射的字符串没必要是多字节字符组。
函数原型:int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cchMultiByte, LPWSTR lpWideCharStr, int cchWideChar);
WideCharToMultiByte
函数功能:该函数映射一个unicode字符串到一个多字节字符串。
函数原型:int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPWSTR lpWideCharStr, int cchWideChar, LPCSTR lpMultiByteStr, int cchMultiByte, LPCSTR lpDefaultChar, PBOOL pfUsedDefaultChar );
- CFile的Unicode宽字符写文件
- CFile 写 Unicode 形式的 CString 到 ANSIC 文件
- MFC UNICODE项目CFILE写html文件乱码解决
- VC++unicode项目使用CFile类写txt日志文件
- MFC Unicode CFile 不能写汉字的问题
- VC中使用CFile正确的追加写中文数据到文件不出现乱码-unicode字符集
- Cfile.flush(),快速写文件
- CFile以追加的的方式写文件
- CFile以追加的的方式写文件
- CFile::write()写入txt文件的CString字符
- MFC使用CFile读写Unicode字符集文件
- MFC使用CFile读写Unicode字符集文件
- CFile针对不同格式(ANSI,UNICODE)的文档写操作方法
- 用CFile 快速反复打开已unicode编码的文件名的文件,会出现assert失败
- 不要再纠结在Unicode下含有CString的结构体CFile文件的输入输出了
- CFile 写文件时,清空文件
- VC++ unicode下读取unicode CFile::typeUnicodetxt的数据
- 日志文件支持unicode字符的做法
- The Privates of Silicon Valley
- OpenGL 创建一个CEGUI窗口
- wamp建站
- 中毒后回收站我的文档出错解决方法
- 微机原理课程设计32位汇编学习之三(显示字符串)
- CFile的Unicode宽字符写文件
- 分析CEGUI的最小资源定制
- Spring工作原理
- Windows下服务管理cmd
- 微机原理课程设计32位汇编学习之四(显示字符串之特殊字体)
- 同步/异步与阻塞/非阻塞的区别
- 建立C++的dll
- VMWare与主机连接并接入Internet
- FreeGlut 框架