4

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.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Chicko Bueno
  • 337
  • 2
  • 11
  • 25
  • possible duplicate of [convert CString to const char*](http://stackoverflow.com/questions/859304/convert-cstring-to-const-char) – ChrisN Mar 31 '11 at 10:01

5 Answers5

9

CString casts to const char * directly

CString temp;
temp = "Wow";
const char * foo = (LPCSTR) temp;
printf("%s", foo);

will print 'foo'

Newer version of MFC also support the GetString() method:

CString temp;
temp = "Wow";
const char * foo = temp.GetString();
printf("%s", foo);
Gregor Brandt
  • 7,659
  • 38
  • 59
3

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:

  • If you have the UNICODE preprocessor symbol defined (i.e., if TCHAR is wchar_t), use the CT2CA or CW2CA macro.
  • If you don't (i.e., if TCHAR is char), CString already has an operator to convert to char const* implicitly (see CSimpleStringT::operator PCXSTR).
ildjarn
  • 62,044
  • 9
  • 127
  • 211
0

If your application is not Unicode, you can simple typecast to const char *. Use the GetBuffer() method if you need a char * 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 like MultiByteToWideChar().)

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
-1

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:

char * convertToCharArr(CString str) {
    int x = 0;
    string s = "";
    while (x < str.GetLength()) {
        char c = str.GetAt(x++);
        s += c;
    }
    char * output = (char *)calloc(str.GetLength() + 1, sizeof(char));
    memcpy(output, s.c_str(), str.GetLength() + 1);
    return output;
}
joker
  • 3,416
  • 2
  • 34
  • 37
-1

First : define char *inputString; in your_file.h

Second : define in yourFile.cpp : CString MyString;

MyString = "Place here whatever you want";

inputString = new char[MyString.GetLength()];
inputString = MyString.GetBuffer(MyString.GetLength());

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.

  • The last 2 code lines ("sentences" as were named above) are bad programming. The first allocate a char array and copy its address in inputString . And the second code line will overwrite with the buffer allocated for MyString (pointer assignment is executed not array copying). Memory allocated by alloc is lost. If MyString is modified and so its buffer is freed and reallocated then inputString contains an invalid pointer. I will not comment on how much new char[] and CString allocate. So this is a very bad answer. – Claudiu Cruceanu Feb 03 '23 at 10:04