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

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