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());