Solved

Conversion from CString to char *s [ C style string]

Posted on 2004-08-03
6
390 Views
Last Modified: 2008-01-09
Hi folks:

How do I convert  a CString to char *  [C- style string]? Thanks in advace
0
Comment
Question by:rxraza
  • 3
  • 2
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
To simply point to the contents of the string:

CString str;
str = "test";

const char *strPtr;
strPtr = str;

If you want to copy contents to a char array:

char buffer[200];  // arbitrary size, you choose
strcpy(buffer, str);
0
 
LVL 13

Accepted Solution

by:
SteH earned 50 total points
Comment Utility
CString str = "What ever you want";

char pString[200];

strcpy (pString, str.operator LPCTSTR ()); // or use operator LPCTSTR () whenver you need a const C style string.

To get a pointer to a buffer you are aloowed to modify use
str.GetBuffer () ; // or GetBufferSetLength ()
and when finished using this buffer call
str.ReleaseBuffer (-1); // -1 if the string is 0 terminated else you need to supply the number of valid chars.
0
 

Author Comment

by:rxraza
Comment Utility
I like operator LPCTSTR() approach better.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Expert Comment

by:SteH
Comment Utility
In the comment from  jaime_olivares the operator LPCTSTR () will be called as well. In his pointer assignment
strPtr= str;
it will be used implicitely.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
>In the comment from  jaime_olivares the operator LPCTSTR () will be called as well. In his pointer assignment
It is not necessary because in my example strPtr is of type: const char *
Try to compile and gives you no warning

In steh comment:
strcpy (pString, str.operator LPCTSTR ()); // or use operator LPCTSTR () whenver you need a const C style string.

str.operator LPCTSTR(),   or better (LPCTSTR)str     is not necessary because second argument of strcpy expect a const char *, and LPCTSTR returns a const char *
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
The intention of my comment was to make rxraza aware that
CString str ("xxxx");
const char* pStr = str; // this line will call operator LPCTSTR () implicitly.

You don't need to write it explicitly. But since it is called why don't write it to show that a conversion/casting is happening here.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 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

12 Experts available now in Live!

Get 1:1 Help Now