Question on converting CString to string

Hi there,
         How can I convert a CString to string (char *str or
char str[20])??

Any help offered is very much appreciated!!!
justinngAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
stkulConnect With a Mentor Commented:

say you have
CString cstr;

try this.
(LPTSTR)(LPCTSTR)cstr.
0
 
wyy_cqCommented:
function char* CString::GetBuffer(int) will help you,
but when you call this function the cstring instance's buffer length will be fixed.
you need to call CString::ReleaseBuffer(...);when you finish the operation.

// example for CString::GetBuffer
CString s( "abcd" );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
LPTSTR p = s.GetBuffer( 10 );
strcpy( p, "Hello" );    // directly access CString buffer
s.ReleaseBuffer( );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif

0
 
GGRUNDYCommented:
Also don't forget that CString automatically casts to
const char* so it is fairly easy to include in an argument
list where a const char* was expected
i.e.

CString sString("abcd");
char zMyString[20];

strcpy(zMyString, sString); //as long as you are sure it will fit

also in the case of varargs you can explicity cast it

TRACE("sString: %s\n", (LPCSTR)sString);

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
justinngAuthor Commented:
Hi thanks but I prefer GGRUNDY's solution...
0
 
wyy_cqCommented:
auto convert it only can be converted int const char*.
if you want to do this :
CString s;
sprintf(s,"%d",100);

you will not be permitted.
GetBuffer(...) can return "char*" not the "const char*" .

i hope you shall browse the sample code again.
CString s( "abcd" );
LPTSTR p = s.GetBuffer( 10 );
strcpy( p, "Hello" );    // directly access CString buffer
s.ReleaseBuffer( );

your question is "convert to string" not "convert to const string".
so i think my answer is correct.



0
 
GGRUNDYCommented:
Hey justinng, I'm sure you are probably trying to do
me a favour by rejecting wyy_cq's answer,
BUT points ain't really the point. Helping is the
point. Please accept Wyy_cq's answer and let's
get this puppy off the front page and into the
solved basket.
0
 
justinngAuthor Commented:
heheee... okie... so I'll be waiting for any one of you to throw this puppy into the solved basket then... ha!
Thanks GGRUNDY!!!
0
 
justinngAuthor Commented:
anyway thanks for letting me know the 2 different solutions for "convert to string" and "convert to const string". Thanks to you all!!! Now at least I know the difference :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.