Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting String to Cstring in C++

Tags:

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?

like image 488
teamaster Avatar asked Aug 06 '12 01:08

teamaster


People also ask

How do you convert std::string to CString?

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.

Is CString same as string?

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.

Can I use CString in C?

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.


1 Answers

.c_str() returns a const char*. If you need a mutable version, you will need to produce a copy yourself.

like image 90
Yann Ramin Avatar answered Sep 18 '22 12:09

Yann Ramin