问题
I would like to convert wstring to wchar_t*. I have tried everything what i know, please help. I would like to convert wstring to wchar_t*.
回答1:
Did you try reading the reference
const wchar_t* wcs = s.c_str();
回答2:
There is no way to convert wstring
to wchar_t*
but you can convert it to const wchar_t*
which is what answer by K.Kirsz says.
This is by design because you can access a const pointer but you shouldn't manipulate the pointer. See a related question and its answers.
The best bet is to create a new string using _wcsdup and access the non const buffer, an ascii example is given there.
For unicode:
wstring str = L"I am a unicode string";
wchar_t* ptr = _wcsdup(str.c_str());
// use ptr here....
free(ptr); // free memory when done
来源:https://stackoverflow.com/questions/44985451/how-to-convert-wstring-to-wchar-t-c