How do I convert CString into const char *? I have tried everything found on the internet but I still cant convert them.
Please help.
Thank you.
How do I convert CString into const char *? I have tried everything found on the internet but I still cant convert them.
Please help.
Thank you.
First : define char *inputString; in your_file.h
Second : define in yourFile.cpp : CString MyString;
The last two sentences convert a CString variable to a char*; but be carefull, with CString you can hold millons of charcteres but with char* no. You have to define the size of your char* varible.
If your application is not Unicode, you can simple typecast to
const char *
. Use theGetBuffer()
method if you need achar *
that you can modify.If your application is Unicode and you really want a
char *
, then you'll need to convert it. (You can use functions likeMultiByteToWideChar()
.)Short answer: Use the
CT2CA
macro (see ATL and MFC String Conversion Macros). This will work regardless of your project's 'Character Set' setting.Long answer:
UNICODE
preprocessor symbol defined (i.e., ifTCHAR
iswchar_t
), use theCT2CA
orCW2CA
macro.TCHAR
ischar
), CString already has an operator to convert tochar const*
implicitly (seeCSimpleStringT::operator PCXSTR
).I know it's late, but I couldn't use the marked-as-answer solution. I search all over the internet and nothing worked for me. I muscled through it to get a solution:
CString casts to const char * directly
will print 'foo'
Newer version of MFC also support the GetString() method: