Solved

Question on converting CString to string

Posted on 1998-11-14
8
268 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
ID: 1324715
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
ID: 1324716
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
ID: 1324717
Hi thanks but I prefer GGRUNDY's solution...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:wyy_cq
ID: 1324718
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
 
LVL 3

Expert Comment

by:GGRUNDY
ID: 1324719
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
ID: 1324720
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
ID: 1324721
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
ID: 1324722

say you have
CString cstr;

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

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Doc'in system (example?) BA 7 120
abstract class with all non abstract mentods 6 84
NotAlone Challenge 20 88
canBalance challenge 34 104
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 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