Converting System::String to Const Char *
c++, char, constants, string
Solution
It's simple!
As you're using managed C++, use the include and operate like:
#include <msclr/marshal.h>
...
void someFunction(System::String^ oParameter)
{
msclr::interop::marshal_context oMarshalContext;
const char* pParameter = oMarshalContext.marshal_as<const char*>(oParameter);
// the memory pointed to by pParameter will no longer be valid when oMarshalContext goes out of scope
}
Problem
I am using Visual C++ 2008's GUI creator to make a user interface. When a button is clicked, the following function is called. The content is supposed to create a file and name the file after the contents of the textbox "Textbox' with '.txt' at the end. However, that leads me to a conversion error. Here is the code: ` private: System::Void Button_Click(System::Object^ sender, System::EventArgs^ e) { ofstream myfile (Textbox->Text + ".txt"); myfile.close(); } ` Here is the error: error C2664: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'System::String ^' to 'const char *' How can I do a conversion to allow this to go through?