Solved

Question on converting CString to string

Posted on 1998-11-14
8
260 Views
Last Modified: 2013-11-20
Hi there,
         How can I convert a CString to string (char *str or
char str[20])??

Any help offered is very much appreciated!!!
0
Comment
Question by:justinng
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 2

Expert Comment

by:wyy_cq
Comment Utility
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
 
LVL 3

Expert Comment

by:GGRUNDY
Comment Utility
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
 

Author Comment

by:justinng
Comment Utility
Hi thanks but I prefer GGRUNDY's solution...
0
 
LVL 2

Expert Comment

by:wyy_cq
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 3

Expert Comment

by:GGRUNDY
Comment Utility
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
 

Author Comment

by:justinng
Comment Utility
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
 

Author Comment

by:justinng
Comment Utility
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
 

Accepted Solution

by:
stkul earned 10 total points
Comment Utility

say you have
CString cstr;

try this.
(LPTSTR)(LPCTSTR)cstr.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now