1. 程式人生 > >strcpy、wcscpy與_tcscpy(給WCHAR賦值)

strcpy、wcscpy與_tcscpy(給WCHAR賦值)

strcpy、wcscpy與_tcscpy

C++標準庫函式提供了字元和字串的操作函式,並提供了其UNICODE版本,如:

  1. char *strcpy(char *strDestination, const char *strSource);  
  2. wchar_t *wcscpy(wchar_t *strDestination, const wchar_t *strSource); 

wcscpy()即為strcpy()的寬字元版本,與_T類似的,Visual C++提供了類似的同名函式:

  1. #ifdef  UNICODE   
  2.     #define _tcscpy     wcscpy  
  3. #else  
  4.     #define _tcscpy     strcpy  
  5. #endif 

因此我們建議這樣書寫程式碼:

  1. TCHAR src[] = _T("學習C++");  
  2. TCHAR dest[20];  
  3. _tcscpy(dest, src); 

比如,在使用printf()的時候,我會嘗試使用_tprintf()。

同樣的版本問題一樣會困擾著main()函式:

  1. main( int argc, char *argv[ ], char *envp[ ]);  
  2. 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