Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert CString to character array?

How to convert CString in MFC to char[] (character array)

like image 527
yesraaj Avatar asked Apr 24 '09 09:04

yesraaj


People also ask

How do I copy a Cstring to a char array?

Method 1: A way to do this is to copy the contents of the string to char array. This can be done with the help of c_str() and strcpy() function of library cstring.

How do I convert a single string to a char in C++?

C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0'). It returns a null pointer to the string.


1 Answers

You use CString::GetBuffer() to get the TCHAR[] - the pointer to the buffer. If you compiled without UNICODE defined that's enough - TCHAR is same as char, otherwise you'll have to allocate a separate buffer and use WideCharToMultiByte() for conversion.

like image 120
sharptooth Avatar answered Sep 20 '22 15:09

sharptooth