CString中GetBuffer函数的应用

说明:MFC函数
  所属类:CString ,CBookMark,CBaseAllocator
  介绍,CString:GetBuffer;
  MSDN对该函数的解释:
  This method retrieves a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.
  If nMinBufLength is greate than the length of the current buffer, the call to GetBuffer will destroy the current buffer. Replace it with a buffer of the requested size, and reset the reference count to zero. If you have previously called LockBuffer on this buffer, you will lose the lock on the buffer.
  这个函数是为一个CString对象重新获取其内部字符缓冲区的指针,返回的LPTSTR为非const的,从而允许直接修改CString中的内容!如果nMinBufLength 比当前buffer大,那么就调用ReleaseBuffer函数去释放当前的Buffer,用一个被请求的大小去覆盖这个buffer, 重新设定计数器为0,如果在这之前你在这个buffer中调用了LockBuffer,那么你将失去你当前锁定的buffer
  说明:
  If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString methods. 
  The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.
  The buffer memory will be freed automatically when the CString object is destroyed. 
  Note that, if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer, which will perform a _tcslen on the buffer to determine its length.
  如果你使用这个指向由GetBuffer所改变返回的字符串内容,那么在你使用CString其他CString方法之前你必须调用ReleaseBuffer
  在调用ReleaseBuffer函数之后GetBuffer中的内容将无效(也就是销毁)
  当这个CString被销毁的时候,这个buffer所占用的内存将被自动释放
  注意这个: 如果你知道了这个字符串的长度,你不可以直接添加NULL字符了事,当你使用ReleaseBuffer的时候你无论如何详细说明字符串的结束位置,如果你仅仅添加了一个NULL字符结束符给这个字符串,你应该给ReleaseBuffer传递一个-1 当这个函数结束的时候_tcslen 将决定这个buffer的长度
  例子:
  // example for CString::GetBuffer
  CString s( "abcd" );//定义一个CString s并且初始化为abcd
  #ifdef _DEBUG
  afxDump << "CString s " << s << "/n";
  #endif

 

       //定义一个指针指向LPTSTR并接受GetBuffer所返回的地址    LPTSTR p = s.GetBuffer( 10 );

       // directly access CString buffer//使用Istrcpy将Hello]复制到该buffer中
  lstrcpy( p, _T("Hello") );
     
  s.ReleaseBuffer( );//释放buffer
 
  #ifdef _DEBUG
  afxDump << "CString s " << s << "/n";这时候s="Hello";
  #endif
  这是一个非常容易被用错的函数,主要可能是由于大家对它的功能不太了解。其实点破的话,也不是那么深奥。
  GetBuffer(int size)是用来返回一个你所指定大小可写内存的成员方法。它和被重载的操作符LPCTSTR还是有点本质区别的,LPCTSTR是直接返回一个只读内存的指针,而GetBuffer则是返回一个可以供调用者写入的内存,并且,你可以给定大小。下面是个简单的,但也是非常典型的例子:
  int readFile(CString& str, const CString& strPathName)
  {
  FILE* fp = fopen(strPathName, "r"); // 打开文件
  fseek(fp, 0, SEEK_END);
  int nLen = ftell(fp); // 获得文件长度
  fseek(fp, 0, SEEK_SET); // 重置读指针
  char* psz = str.GetBuffer(nLen);
  fread(psz, sizeof(char), nLen, fp); //读文件内容
  str.ReleaseBuffer(); //千万不能缺少
  fclose(fp);
  }
  上面的函数是GetBuffer函数最典型的用法了,其实它就相当于申请一块nLen大小的内存,只不过,这块内存是被引用在CString对象的内部而已,这是非常有效的一种用法,如果不直接用GetBuffer函数来申请的话,那么你必须用new操作符(或者malloc()函数)在CString的外部申请,然后再将申请的内存拷贝到CString对象中,显然这是一个非常冗余的操作,会使你函数的效率大大下降。
  ReleaseBuffer函数是用来告诉CString对象,你的GetBuffer所引用的内存已经使用完毕,现在必须对它进行封口,否则CString将不会知道它现在所包含的字符串的长度,所以在使用完GetBuffer之后,必须立即调用ReleaseBuffer函数重置CString的内部属性,其实也就是头部信息。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
随笔 - 764 文章 - 3 评论 - 196 CString,string,char*之间的转换(转) 这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差。string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的;char*是从学习C语言开始就已经和我们形影不离的了,有许多API都是以char*作为参数输入的。所以熟练掌握三者之间的转换十分必要。 以下我用简单的图示指出三者之间的关系,并以标号对应转换的方法。 1 string to CString CString.format("%s",string.c_str()); 2 CString to string string str(CString.GetBuffer(str.GetLength())); 3 string to char * char *p=string.c_str(); 4 char * to string string str(char*); 5 CString to char * strcpy(char,CString,sizeof(char)); 6 char * to CString CString.format("%s",char*); CString的format方法是非常好用的。string的c_str()也是非常常用的,但要注意和char *转换时,要把char定义成为const char*,这样是最安全的。 以上函数UNICODE编码也没问题:unicode下照用,加个_T()宏就行了,像这样子_T("%s") 补充: CString 可能是 CStringW/CStringA,在与 string 转换时,如果是 CStringW,还涉及编码转换问题。下面以 CStringA 来说明。 1 string to CString CString.format("%s",string.c_str()); CStringA = string.c_str() 就可以了 2 CString to string string str(CString.GetBuffer(str.GetLength())); GetBuffer 有参数的话,可能导致内部的分配空间动作,要进行后续 ReleaseBuffer 操作。 string = CStringA string = CStringA.GetBuffer(); 3 string to char * char *p=string.c_str(); 4 char * to string string str(char*); 5 CString to char * strcpy(char *,CString,sizeof(char)); 按照 3 风格,这里应该 char * = CStringA; 或者 char *p = CStringA.GetBuffer(); 6 char * to CString CStringA = char * 就可以了

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值