String Help
Hi,
I am currently retrieving a string value from an OLE DB rowset like this:
for (int i=0;i<lNumCols;i++)
{
szTemp.Format(_T("%d"),pBindings[i].cbMaxLen);
szTemp = "%" + szTemp + "s";
szTemp.Format(szTemp,&pBuffer[pBindings[i].obValue]);
szTemp.TrimLeft();
szTemp.TrimRight();
But this does not work very well and causes random errors at the .Format part.
Can someone show me a better way to do this?
Thanks for any help.
[484 byte] By [
Phunction] at [2007-11-18 13:39:53]

# 3 Re: String Help
Well, I removed a Format and just used %s and it seems to work.
I was getting an assertion error in strcore.cpp, but this seems to have fixed the error. Maybe I was trying to do to many .formats at once?
pBuffer and pBindings are declared as:
BYTE *pBuffer;
DBBINDING* pBindings;
# 4 Re: String Help
Well, I believe I've got your answer...
there's an eternal char* in CString that you are confusing...
inside of CString::FormatV there are these lines:
GetBuffer(nMaxLen);
VERIFY(_vstprintf((char*)buffer, argString, argListSave) <= bufferSize);
ReleaseBuffer();
argString is Format param 1
and buffer is the internal CString char* buffer...
Can you see the problem?
szTemp.Format(szTemp,&pBuffer[pBindings[i].obValue]);
operator LPCTSTR() of CString returns the a pointer to the interal char* of the CString object...
which if you obmit everything else and just use some psudo code, your code would look like this:
strcpy(szTemp.buffer, szTemp.buffer, size);
So what you have a string being copied onto itself...
But why so random?
Well besides corruption from the copy of memory you've got this:
"GetBuffer()"...
In CString GetBuffer may or may not reallocate memory...
so why buffer you are coping into may be valid, the argString you are copying from may no longer be...
But it cleared its self up when you removed the pervious Format?
Probably because the string was reallocated down to what it needed in the first call and when you called the second one, you forced a reallocation...
But without the first format, the buffer probably wont need to reallocate...
Check out the CString::Format Function in the source code.
I hope this has been helpful...I'm running alot of guess work since I'm still at work and can't check it, but I'm pretty sure this is what's giving you grief :)
Good luck
EDIT: The actual names in FormatV may not be the same :) but the lines are at the bottom of FormatV in "StrEx.cpp", sorry for the confusion...