VC++常用数据类型及其操作详解及Unicode编程

   一点经验。在VC2005编程环境中,要有UNICODE意识。一般安全的做法是用TCHAR代替char使用,为字符串赋值时,用_T(" ")代替" "。因为在tchar.h中有如下声明:

#ifdef _UNICODE
typedef wchar_t     TCHAR;
#define __T(x)      L##x
#define _T(x)       __T(x)
#else
#define __T(x)      x
typedef char            TCHAR;
#endif

T(_T宏)表示兼容ANSI和Unicode


If you are using Unicode or MBCS then you need to be careful when writing ASCII files. The safest and easiest way to write text files is to use the CStdioFile class provided with MFC. Just use the CString class and the ReadString and WriteString member functions and nothing should go wrong. However, if you need to use the CFile class and it's associated Read and Write functions, then if you use the following code:

CFile file(...);
CString str = _T("This is some text");
file.Write( str, (str.GetLength()+1) * sizeof( TCHAR ) );
instead of

CStdioFile file(...);
CString str = _T("This is some text");
file.WriteString(str);
then the results will be Significantly different. The two lines of text below are from a file created using the first and second code snippets respectively:

但是在使用CStdioFile::WriteString(str)时,若str包含亚洲字体如中文、日语等(在 Unicode 环境下),这些字体确显示不出来,现在我也没找到解决的办法,不知哪位知道告诉我一声,另:
Be careful when performing operations that depend on the size or length of a string. For instance, CString::GetLength returns the number of characters in a string, NOT the size in bytes. If you were to write the string to a CArchive object, then you would need to multiply the length of the string by the size of each character in the string to get the number of bytes to write:

CString str = _T("Hello, World");
archive.Write( str, str.GetLength( ) * sizeof( TCHAR ) );

CFile对象的Write也一样。

以下三篇文章值得一看

1.VC++常用数据类型及其操作详解

2.VC++的Unicode编程

3.http://www.codeproject.com/cpp/unicode.asp


原文链接: http://blog.csdn.net/augusdi/article/details/4693890

转载于:https://my.oschina.net/chen106106/blog/45053

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值