c语言char转cstring,如何将CString类型的变量赋给char*类型的变量

1、GetBuffer函数使用CString::GetBuffer函数。char *p;CString str="hello";p=str.GetBuffer(str.GetLength());str.ReleaseBuffer();将CString转换成char * 时CString str("aaaaaaa");strcpy(str.GetBuffer(10),"aa");str.Rel...
摘要由CSDN通过智能技术生成

1、GetBuffer函数

使用CString::GetBuffer函数。

char *p; CString str="hello"; p=str.GetBuffer(str.GetLength()); str.ReleaseBuffer();

将CString转换成char * 时

CString str("aaaaaaa");

strcpy(str.GetBuffer(10),"aa");

str.ReleaseBuffer();

当我们需要字符数组时调用GetBuffer(int

n),其中n为我们需要的字符数组的长度.使用完成后一定要马上调用ReleaseBuffer();

还有很重要的一点就是,在能使用const char *的地方,就不要使用char *

2、memcpy

CString mCS=_T("cxl"); char mch[20]; memcpy(mch,mCS,20);

3、用LPCTSTR强制转换: 尽量不使用

char *ch; CString str; ch=(LPSTR)(LPCTSTR)str;

CString str = "good";

char *tmp;

sprintf(tmp,"%s",(LPTSTR)(LPCTSTR)str);

4、

CString Msg; Msg=Msg+"abc"; LPTSTR lpsz; lpsz = new

TCHAR[Msg.GetLength()+1]; _tcscpy(lpsz, Msg); char * psz; strcpy(psz,lpsz);

CString类向

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值