关于CString写入文件的操作
如:
=================================================================
=================================================================
这是因为:在Unicode下,1个字符 = 2字节,而第2个参数要求传入的是字节
数。
当然如果我们写的是Unicode的,就会出错。所以还要加入一个格式:
就可以了。这两个放在头部提示文本编辑器使用unicode解码。
如果项目用的是unicode的话那么实际上是CStringW类型,这个时候向string转换的时候,编译器会报错,const char* 无法转换为const w_char *,这个时候只能这个做了。
1
//
tmp1极为CStringW宽字符变量
2 // 转换为psText的char* 变量了
3
4 #ifdef UNICODE
5 DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, tmp1.GetBuffer( 0), - 1, NULL, 0, NULL, FALSE);
6 char *psText;
7 psText = new char[dwNum];
8 if (!psText)
9 delete []psText;
10 WideCharToMultiByte(CP_OEMCP, NULL, tmp1.GetBuffer( 0), - 1, psText, dwNum, NULL, FALSE);
11 #endif
2 // 转换为psText的char* 变量了
3
4 #ifdef UNICODE
5 DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, tmp1.GetBuffer( 0), - 1, NULL, 0, NULL, FALSE);
6 char *psText;
7 psText = new char[dwNum];
8 if (!psText)
9 delete []psText;
10 WideCharToMultiByte(CP_OEMCP, NULL, tmp1.GetBuffer( 0), - 1, psText, dwNum, NULL, FALSE);
11 #endif