I have a string to convert, string = "apple"
and want to put that into a C string of this style, char *c
, that holds {a, p, p, l, e, '\0'}
. Which predefined method should I be using?
Converting a std::string to a CString is as simple as: std::string stdstr("foo"); CString cstr(stdstr. c_str()); This works for both UNICODE and MBCS projects.
The two headers are completely different. cstring is inherited from C and provides functions for working with C-style strings (arrays of char terminated by '\0' ). string was born in C++ and defines the std::string class along with its non-member functions. It compares the numeric values of the characters.
CString does not store character data internally as a C-style null-terminated string. Instead, CString tracks the length of character data so that it can more securely watch the data and the space it requires. CString does accept C-style strings, and provides ways to access character data as a C-style string.
.c_str()
returns a const char*
. If you need a mutable version, you will need to produce a copy yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With