How to convert CString to char*?
-
did you try (char *)csMyCString ? -c
To explain Donald Knuth's relevance to computing is like explaining Paul's relevance to the Catholic Church. He isn't God, he isn't the Son of God, but he was sent by God to explain God to the masses.
/. #3848917 -
did you try (char *)csMyCString ? -c
To explain Donald Knuth's relevance to computing is like explaining Paul's relevance to the Catholic Church. He isn't God, he isn't the Son of God, but he was sent by God to explain God to the masses.
/. #3848917It depends - do you want a constant char *? Then doing as Chris said works fine (and usually happens automatically.) If you want a char * that's not constant, try CString::GetBuffer (and CString::ReleaseBuffer when you're done with the char *.) Good ol' strcpy to a char array also works. :) Even if you win the rat race, you're still a rat.
-
If you only need a constant string, you can use the implicit LPCTSTR operator of CString, which under ANSI builds will give you a const char*. If you need a non-constant string, then you can use the GetBuffer() method to get direct access to the underlying buffer, and then ReleaseBuffer() when you have finished with it. Dave
-
Yeah: 1) CString csTemp = "bla bla"; char *szResult; szResult = new char[csTemp.GetLength() + 1]; strcpy(szResult,csTemp); or csTemp.GetBuffer(0); //This returns char*
in MSDN there is one example given Example The following example demonstrates the use of CString::operator LPCSTR. // If the prototype of a function is known to the compiler, // the LPCTSTR cast operator may be invoked implicitly CString strSports(_T("Hockey is Best!")); TCHAR sz[1024]; lstrcpy(sz, strSports); // If the prototype isn't known, or is a va_arg prototype, // you must invoke the cast operator explicitly. For example, // the va_arg part of a call to sprintf() needs the cast: sprintf(sz, "I think that %s!\n", (LPCTSTR) strSports); // while the format parameter is known to be an LPCTSTR and // therefore doesn't need the cast: sprintf(sz, strSports); // Note that some situations are ambiguous. This line will // put the address of the strSports object to stdout: cout << strSports; // while this line will put the content of the string out: cout << (LPCTSTR) strSports;