Главная страница | назад





Article #16757: Converting string type to char*

 Question and Answer Database

FAQ1757C.txt   Converting string type to char*
Category   :C/C++ Language Issues
Platform    :All
Product    :C++Builder  1.x

Question:


I am using a third party library, and some of the functions 
return value of type string. 

// for example
string GetText(); 

So to process the returned string I have:

string buf;
buf = GetText();

This works just fine. But I can't do anything with buf.
If I type: 

MessageBox(NULL, buf, "Data", NULL);
 
I get the error message 
cann't convert 'string' to 'const char*'
I get the same error message if I:

strcpy(str,buf)
or 
strlen(buf);

I can't find anywhere how 'string' is defined. I know it's not an 
LPCSTR or LPSTR , and I don't think it meant to be the class 
String.

Can you help? and how can I get buf to work with, let's say, the 
strcpy(); 

Answer:


String is a string. That is, an STL string. It doesn't have a 
built in const char * operator (LPCTSTR operator). To use it in 
such situations you use its c_str function.

string myString("I'm a string");

MessageBox(myString.c_str());

7/2/98 10:32:32 AM
 

Last Modified: 01-SEP-99