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
Printable View
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
The following works fine for me.
Prints out:Code:void CTraceTestDlg::OnBnClickedOk()
{
CString sDirectory( _T("D:\\Projects") );
TRACE( sDirectory );
TRACE( _T("\nlength: %d\n"), sDirectory.GetLength( ) );
}
What version of Visual C++ are you using?Code:D:\Projects
length: 11
Did you use GetBuffer to fill the string (and not use releasebuffer to mark the length of the string) ?Quote:
how come? Why the length is zero?
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!
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.
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.