GetBuffer解析

来源:互联网 发布:软件测试各个阶段 编辑:程序博客网 时间:2024/06/01 08:10
CString str=_T("abc");LPTSTR lpstr=str.GetBuffer(10);lstrcpy(lpstr,_T("01234"));TRACE(str);//输出01234


如果你对GetBuffer返回的指针内容修改了,那么在你在使用CString的方法之前你必须调用ReleaseBuffer

CString str=_T("abc");//长度3LPTSTR lpstr=str.GetBuffer(10);lstrcpy(lpstr,_T("01234"));str.ReleaseBuffer();//str+="hello";int iLen=str.GetLength();CString strLen;strLen.Format(_T("%d"),iLen);TRACE(str);TRACE(_T("\n"));TRACE(strLen);

输出结果:

str:01234
长度:3

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CString str=_T("abc");LPTSTR lpstr=str.GetBuffer(10);lstrcpy(lpstr,_T("01234"));str.ReleaseBuffer();//str+="hello";int iLen=str.GetLength();CString strLen;strLen.Format(_T("%d"),iLen);TRACE(str);TRACE(_T("\n"));TRACE(strLen);
输出结果:

str:01234
长度:5

——————————————————————————————————————————————————————————————

CString str=_T("abc");LPTSTR lpstr=str.GetBuffer(10);lstrcpy(lpstr,_T("01234"));//str.ReleaseBuffer();str+="hello";int iLen=str.GetLength();CString strLen;strLen.Format(_T("%d"),iLen);TRACE(str);TRACE(_T("\n"));TRACE(strLen);
输出结果:

str:012hello
长度:8

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CString str=_T("abc");LPTSTR lpstr=str.GetBuffer(10);lstrcpy(lpstr,_T("01234"));str.ReleaseBuffer();str+="hello";int iLen=str.GetLength();CString strLen;strLen.Format(_T("%d"),iLen);TRACE(str);TRACE(_T("\n"));TRACE(strLen);
输出结果:

str:01234hello
长度:10

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

我只是测试了这其中一项,有什么不对的地方,欢迎指正!


原创粉丝点击