|
-
August 29th, 2010, 07:51 PM
#1
CString's GetLength mystery
I was testing a variable, the following are two lines:
TRACE(m_strDirectory);
TRACE("\nlength: %d\n", m_strDirectory.GetLength());
and the output is:
D:\Projects
length: 0
how come? Why the length is zero?
Thanks!
Sway
-
August 29th, 2010, 08:14 PM
#2
Re: CString's GetLength mystery
The following works fine for me.
Code:
void CTraceTestDlg::OnBnClickedOk()
{
CString sDirectory( _T("D:\\Projects") );
TRACE( sDirectory );
TRACE( _T("\nlength: %d\n"), sDirectory.GetLength( ) );
}
Prints out:
Code:
D:\Projects
length: 11
What version of Visual C++ are you using?
-
August 30th, 2010, 02:18 AM
#3
Re: CString's GetLength mystery
how come? Why the length is zero?
Did you use GetBuffer to fill the string (and not use releasebuffer to mark the length of the string) ?
-
August 31st, 2010, 09:15 PM
#4
Re: CString's GetLength mystery
Thank you both. Arjay: the VC is 9.0; Skizmo: you are right. I used GetBuffer to fill the string and didn't releaseBuffer. Now it's fine. But can you eleborate on it, or give me a link to read?
Thanks again!
-
August 31st, 2010, 09:22 PM
#5
Re: CString's GetLength mystery
CString maintains an internal count.
Normally the CString constructor or operators like =, += or functions like Format, Insert, Replace etc. are used to modify the CString object. All these methods will update the internal count.
GetBuffer on the other hand exposes the internal buffer in the CString object.
All operations on this buffer are not recorded by the object.
Its only when ReleaseBuffer is called that the internal count is updated.
If you expose the internal buffer using GetBuffer for read only access, it is not necessary to call ReleaseBuffer as nothing is modified.
-
August 31st, 2010, 09:24 PM
#6
Re: CString's GetLength mystery
See the documentation for GetBuffer in msdn.
Unfortunately, the docs for CString kind of suck, so you'll need to speed some time looking around and the different methods.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|