Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a CString 1

Status
Not open for further replies.

drinkmilk

Programmer
May 10, 2005
3
FR
Hi,

I am making a request on a microsoft Access data base:

Code:
CDBVariant varValue;
CString repere;

record.GetFieldValue((short)0,varValue);
repere = *varValue.m_pstring;

everything seems Ok till here when I debug (ex: repere "310.96")

Then I add the repere's value in buffer (which is already containing some text).

buffer = buffer + repere;
buffer = buffer + _T("Any text");

My problem: once I've add "repere" to "buffer", it's not possible to make more adds to "buffer" ("Any text" is NOT in "buffer")

I've also tried this, without any success:

Code:
record.GetFieldValue(_T("repere"),repere1); // repere1 is a CString
repere = repere1;

Precision: if there are no access to the database, there are no problems with the buffer.

I don't know what to do. Has anyone some kind of idea ?

Every kinf of help would be really welcome !

 
My problem: once I've add "repere" to "buffer", it's not possible to make more adds to "buffer"

Perhaps repere holds an unintentional '\0'?

Check that by
Code:
buffer = buffer + (LPCTSTR)repere;
If that solves the problem, then check out why repere gets wrong '\0'.



/Per

www.perfnurt.se
 
Thanks a lot, you got the solution.

I guess that the GetFieldValue function has had '\0', and I don't know why.

But why does a cast to (LPCTSTR) resolves the problem ? I don't get that.
 
But why does a cast to (LPCTSTR) resolves the problem ? I don't get that.

Because LPCTSTR is just plain string (TCHAR*). As such there doesn't exist the concept of multiple '\0's. The string ends simply ends when the first '\0' is encountered.

However, when you do a s1 += s2, where sX are all CStrings, another mechanism is used. The entire buffer of s2 (including erroneous '\0's) is copied over.

As the '\0' gets copied over, whenever you try too look at s1 there'll be a '\0' making stuff believe the string ends there, hence it would appear as you can't add anything to s1 (even though you can), because virtually all print/display string functions looks at strings the old C way (ie looks for the '\0' that terminates the string).

cast to (LPCTSTR)
A minor note: (LPCTSTR) isn't really a casting (there are no string casting in C++), but rather a way to invoke CString's LPCTSTR operator.

/Per

www.perfnurt.se
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top