设为首页 加入收藏

TOP

VC++常用数据类型及其操作详解及Unicode编程
2014-11-23 20:26:39 】 浏览:301
Tags:常用 数据 类型 及其 操作 详解 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中使用Windows管道技术编程

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目