CString 轉換爲const char *

CString轉換爲char *
如果你用的是unicode的話,那麼CString裏面存儲的是wchar_t*,而不是char*。你確定要把CString轉換成char*的話,還要用其他的函數:
const wchar_t* wstr = ( LPCTSTR )name;     //一定得是unicode,否則這句話會錯的
char str[ 20 ] = { 0 };
wcstombs( str, wstr, wcslen( wstr ) );
執行完後,str中的數據就是"111.txt"了。str可以賦值給一個const char*。
注意:如果CString裏有中文的話,在wcstombs前後還應加這麼兩句:
setlocale( LC_ALL, "chs" );
wcstombs( str, wstr, wcslen( wstr ) );
setlocale( LC_ALL, "C" );

string類型轉換爲char *
string str("hello world");
char gs[20];
strcpy(gs, str.c_str());

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章