strcpy、wcscpy與_tcscpy(給WCHAR賦值)
阿新 • • 發佈:2018-12-14
strcpy、wcscpy與_tcscpy
C++標準庫函式提供了字元和字串的操作函式,並提供了其UNICODE版本,如:
- char *strcpy(char *strDestination, const char *strSource);
- wchar_t *wcscpy(wchar_t *strDestination, const wchar_t *strSource);
wcscpy()即為strcpy()的寬字元版本,與_T類似的,Visual C++提供了類似的同名函式:
- #ifdef UNICODE
- #define _tcscpy wcscpy
- #else
- #define _tcscpy strcpy
- #endif
因此我們建議這樣書寫程式碼:
- TCHAR src[] = _T("學習C++");
- TCHAR dest[20];
- _tcscpy(dest, src);
比如,在使用printf()的時候,我會嘗試使用_tprintf()。
同樣的版本問題一樣會困擾著main()函式:
- main( int argc, char *argv[ ], char *envp[ ]);
- wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ]);
再來看_tmain()的定義:
#ifdef UNICODE
#define _tmain wmain
#define _tWinMain wWinMain
#else
#define _tmain main
#define _tWinMain WinMain
#endif