String conversion from System::String ^ to const wchar_t *

人走茶凉 提交于 2021-02-04 08:12:22

问题


I'm trying to open a file using a string parameter, however I'm getting the following error:

error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'System::String ^' to 'const wchar_t *'

How do you convert System::String ^ to const wchar_t *?


回答1:


As Hans points out, simple conversion is necessary. It would look similar to the following:

System::String ^str = L"Blah Blah";
pin_ptr<const wchar_t> convertedValue = PtrToStringChars(str);  // <-- #include <vcclr.h>
const wchar_t *constValue = convertedValue;                     // <-- Unnecessary, but to be completely verbose
void std::basic_ifstream<_Elem, _Traits>::open(constValue, mode, i);


来源:https://stackoverflow.com/questions/22529094/string-conversion-from-systemstring-to-const-wchar-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!