Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Question on converting CString to string

Posted on 1998-11-14
8
267 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
method notes when mouse over in eclipse 5 87
scoreUp challenge 14 63
Unix Command -- Challenging  question 7 95
Increment column based of a FK 8 31
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

840 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