当前位置:网站首页>CString的GetBuffer和ReleaseBuffer使用说明

CString的GetBuffer和ReleaseBuffer使用说明

2022-07-03 15:20:00 宇龍_

先看一段代码:

#include <atlstr.h>

//测试CString

int main(int argc, _TCHAR* argv[])
{
	CStringA strTemp = "555";
	//这里输出为3,不包含结束符
	printf("str=%s,len=%d\n",strTemp.GetBuffer(),strTemp.GetLength());

	LPSTR lpStr = strTemp.GetBuffer(10);
	//这里输出为3,因为对GetBuffer的返回值进行了操作,未调用ReleaseBuffer,GetLength()返回结果将不正确
	strcpy(lpStr,"666666");
	printf("str=%s,len=%d\n",strTemp.GetBuffer(),strTemp.GetLength());

	strTemp.ReleaseBuffer();
	//由于调用了ReleaseBuffer,GetLength()的返回结果就为6
	printf("str=%s,len=%d\n",strTemp.GetBuffer(),strTemp.GetLength());

	system("pause");
	return 0;
}

运行结果:

ReleaseBuffer源码:

   	void ReleaseBuffer(_In_ int nNewLength = -1)
	{
    
原网站

版权声明
本文为[宇龍_]所创,转载请带上原文链接,感谢
https://zhiyu.blog.csdn.net/article/details/124619475