CString的GetBuffer用法

一.函數原型

CString::GetBuffer

LPTSTR GetBuffer( int nMinBufLength );

throw( CMemoryException );

Return Value

An LPTSTR pointer to the object’s (null-terminated) character buffer.

Parameters

nMinBufLength

The minimum size of the character buffer in characters. This value does not include space for a null terminator.

Remarks

Returns 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 you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.

二.函數作用及使用範圍

對一個CString變量,你可以使用的唯一合法轉換符是LPCTSTR,直接轉換成非常量指針LPTSTR-[const] char*)是錯誤的。正確的得到一個指向緩衝區的非常量指針的方法是調用GetBuffer()方法。

GetBuffer()主要作用是將字符串的緩衝區長度鎖定,releaseBuffer則是解除鎖定,使得CString對象在以後的代碼中繼續可以實現長度自適應增長的功能。

CString ::GetBuffer有兩個重載版本:

LPTSTR GetBuffer( );LPTSTR GetBuffer(int nMinBufferLength);

在第二個版本中,當設定的長度小於原字符串長度時,nMinBufLength = nOldLen,該參數會被忽

略,不分配內存,指向原CString;當設定的長度大於原字符串本身的長度時就要重新分配(reallocate)一塊比較大的空間出來。而調用第一個版本時,應如通過傳入0來調用第二個版本一樣。

是否需要在GetBufer後面調用ReleaseBuffer(),是根據你的後面的程序是否需要繼續使用該字符串變量,並且是否動態改變其長度而定的。如果你GetBuffer以後程序自函數就退出,局部變量都不存在了,調用不調用ReleaseBuffer沒什麼意義了。

最典型的應用就是讀取文件:

CFile file;

// FILE_NAME 爲實現定義好的文件名稱

if(file.Open(FILE_NAME,CFile::modeRead))

{

       CString szContent;

       int nFileLength = file.GetLength();

       file.Read(szContent.GetBuffer(nFileLength),nFileLength);

       szContent.ReleaseBuffer();

       // 取得文件內容放在szContent中,我們之後可以對其操作

}

三.測試

以下就CString::GetBuffer,做簡單測試:

測試1

// example for CString::GetBuffer

 

#include <stdio.h>

#include <afx.h>

 

void main(void)

{    

       CString s( "abcd" );

       printf("(1)before GetBuffer:/n");

       printf("CString s.length=%d/n",s.GetLength());

       printf("CString s=%s/n",s);

      

       LPTSTR p = s.GetBuffer( 2 );

      

       printf("(2)after GetBuffer and before ReleaseBuffer:/n");

       printf("LPTSTR p=%s/n",p);    

       printf("p.length=%d/n",strlen(p));

printf("CString s=%s/n",s);      

       printf("CString s.length=%d/n",s.GetLength());

      

 

       s.ReleaseBuffer( );

       printf("(3)after ReleaseBuffer:/n");

       printf("LPTSTR p=%s/n",p);    

       printf("p.length=%d/n",strlen(p));

printf("CString s=%s/n",s);

       printf("CString s.length=%d/n",s.GetLength());    

}

測試結果1

(1)before GetBuffer:

CString s.length=4

CString s=abcd

(2)after GetBuffer and before ReleaseBuffer:

LPTSTR p=abcd

p.length=4

CString s=abcd

CString s.length=4

(3)after ReleaseBuffer:

LPTSTR p=abcd

p.length=4

CString s=abcd

CString s.length=4

Press any key to continue

測試2

LPTSTR p = s.GetBuffer( 2 ); 修改爲:LPTSTR p = s.GetBuffer( 10 );

測試結果同1

測試3

在測試二的LPTSTR p = s.GetBuffer( 10 );後添加  p[5]='f';

測試結果同1

測試4

將測試三的p[5]='f';修改爲p[4]='e';

測試結果4

(1)before GetBuffer:

CString s.length=4

CString s=abcd

(2)after GetBuffer and before ReleaseBuffer:

LPTSTR p=abcde屯屯?

p.length=10

CString s=abcde屯屯?

CString s.length=4

(3)after ReleaseBuffer:

LPTSTR p=abcde屯屯?

p.length=10

CString s=abcde屯屯?

CString s.length=10

Press any key to continue

很顯然(2)after GetBuffer and before ReleaseBuffer: CString s.length=4結果有問題。

注意:以上測試是在_MBCS環境下,如果換成_UNICODE則結果有可能不同。

參考:

CString GetBuffer()

http://blog.csdn.net/hbyh/archive/2007/09/15/1786574.aspx

CStringGetBuffer問題

http://game.tongji.net/thread-379834-1-1.html

CStringGetBuffer

http://www.programfan.com/blog/article.asp?id=40755

CString GetBuffer() and ReleaseBuffer()

http://blog.csdn.net/guanchanghui/archive/2006/09/13/1217096.aspx

CString::GetBuffer()CString::ReleaseBuffer到底有什麼用?

http://topic.csdn.net/t/20060313/22/4612156.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章