分享

将CString类转换成char*类型

 ylw527 2010-09-14
使用强制转换。例如:

CString       theString(       "This       is       a       test "       );
LPTSTR       lpsz       =(LPTSTR)(LPCTSTR)theString;          
char       *buf;      
CString       str       =       "hello ";      
buf       =       (LPSTR)(LPCTSTR)str;
 
CString   str   =   "ABC ";
char*   chArr;
chArr   =   (char*)(LPCTSTR)str;
CString   str( "50 ");
int   nConv   =   atoi(   str   );

使用strcpy。例如:

CString       theString(       "This       is       a       test "       );
LPTSTR       lpsz       =       new       TCHAR[theString.GetLength()+1];
_tcscpy(lpsz,       theString);    
 
char   szBuff[100];
CString   str   =   "123456abc ";
strncpy(   szBuff,   str,   strlen(   str   )   ); 
 或
strncpy(szBuff, str, str.GetLength());   //不要+1了,防越界

使用CString::GetBuffer。例如:

CString       s(_T( "This       is       a       test       "));
LPTSTR       p       =       s.GetBuffer();
//       在这里添加使用p的代码
if(p       !=       NULL)       *p       =       _T( '\0 ');
s.ReleaseBuffer();      
//       使用完后及时释放,以便能使用其它的CString成员函数    
 
CString   str( "this   is   a   test! ");
char*   szBuf   =   str.GetBuffer(   str.GetLenghth()   );
//......
str.ReleaseBuffer();
 
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多