当前位置:编程学习 > VC++ >>

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 its 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也一样。

补充:软件开发 , Vc ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,