From vc6.0 to vs2005 and above, it must be complicated for vs2005 character processing.
In vs2005, there are concepts such as tchar, lpcstr, and cstring.
1. How to compile
What is the simplest method?ProgramUse the cstring of MFC, and add the _ t macro to all character constants.
Cstring can be directly converted to lpcstr,
For example, cstring STR = _ T ("hello ");
Lpcstr S = STR;
Basically, with cstring and _ t macros, you can write the MFC program, so there won't be any problems during compilation.
Okay. If you want to know the principle, let's continue. If you only want to solve the problem, you can skip the following content.
Principle 2
2.1
Because c ++ supports two types of strings: Regular ANSI encoding (using "" packages) and Unicode encoding (using L "" packages ), in this way, there are two sets of string processing functions, such as strlen and wcslen, which are used to process two types of strings respectively.
Microsoft unifies these two sets of character sets and their operations, and controls the actually used character sets through Conditional compilation (through _ Unicode and Unicode macros), so that the _ T ("") such a string corresponds to a function such as _ tcslen.
To store such common characters, tchar is available:
When _ Unicode macro is not defined, tchar = char, _ tcslen = strlen
When _ Unicode macro is defined, tchar = wchar_t, _ tcslen = wcslen
So the omnipotent _ T came, _ T ("hello ");
If you compile a program in ANSI mode, _ t does not actually play any role. If a program is compiled in Unicode mode, the compiler saves the "hello" string in Unicode mode.
In a word, if you are afraid of trouble, simply use _ T (), it will help you with everything, automatically choose to use Char or wchar_t.
Therefore, when writing the vs program, add a _ t before all the character constants without adding white or white.
2.2
Cstring and lpcstr can be assigned values directly to each other (it must be overloaded with "= ").
Cstring STR;
Str. Format (_ T ("x = % d, y = % d"), 1, 2 );
Lpctstr S = _ T ("dddd ");
STR = s;
Afxmessagebox (STR );
All the above programs, the test environment is vs2005
References:
Http://www.cnblogs.com/sunnyjones/archive/2009/02/24/1397538.html
Http://baike.baidu.com/view/2624257.htm
Http://baike.baidu.com/view/2901835.htm