Friday, November 13, 2009

TCHAR.H


3 4



TCHAR.H



This header file contains devices that are intended to make it easy for you to write string analysis and manipulation code, which will work with ANSI, DBCS, or Unicode strings, depending on your project setting. These devices include the following:




  • The TCHAR typedef. Its alias is either char or wchar_t, as previously described. Some types in the Platform SDK are derived from TCHAR, such as LPTSTR and LPCTSTR.


  • The _T (or _TEXT) macros. These produce either narrow or wide character literals or string literals in your source code, depending on your project setting.


  • Run-time library wrappers and other helper functions and macros that select the correct behavior against the TCHAR type. Examples include string pointer arithmetic helpers, such as _tcsinc and _tcsdec, and string function wrappers, such as _tcslen, _tcsclen, _tcscpy, and _tcsncpy.





The following code segment returns the sum of all the character values in the given string. It works correctly for ANSI, DBCS, and Unicode strings and accepts narrow or wide string input depending on your project settings.





double CrossSum(LPCTSTR sArg)
{
double fResult = 0;

for (LPCTSTR sCurChar = sArg; *sCurChar;
sCurChar = _tcsinc(sCurChar))
fResult += _tcsnextc(sCurChar);

return fResult;
}



The TChar.h header file contains interesting tricks and ideas that could be helpful in your own projects. Browse through it some time.



No comments: