-
std::string to char*
I'm pretty noobish when it comes to the string class - I used to use char* all the time until otherwise informed. So how would I convert a string to char*? Although best avoided, would this method *work*:
Code:
string str;
char* other_str;
cin >> str;
other_str = new char[str.size() + 1]; // would this work?
strncpy (other_str, reinterpret_cast<char*> (str), str.size());
delete [] other_str;
// etc...
I just tapped that out off the top of my head so it's probably either wrong or very noob-esque.
I would like to know how to do this in case I need to, for example, read user input into a string instance, then pass it to an fopen call (or whatever takes a char*).
-
-
c_str()? I'll look it up in MSDN. Thanks :)
-
don't use msdn... use http://www.cppreference.com (they only have standard things... MSDN is sometimes hard to understand, and has alot of non-standard, windows-only things)
-
Lol you were right, MSDN's answer to it was about 4 lines long and almost illegible.