System Conversion :: String to Const Char *

I am using the Visual C ++ 2008 GUI Creator to create a user interface. When the button is pressed, the next function is called. It is assumed that the contents will create a file and name the file after the contents of the Text Box text box with ".txt" at the end. However, this 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 the conversion to resolve this?

+5
source share
6 answers

I would use sorting:

//using namespace System::Runtime::InteropServices;
const char* str = (const char*)(void*)
       Marshal::StringToHGlobalAnsi(Textbox->Text);
// use str here for the ofstream filename
Marshal::FreeHGlobal(str);

But note that you only use Ansi strings. If you need Unicode support, you can use the broadband STL class wofstreamand PtrToStringChars( #include <vcclr.h>) to convert from System::String. In this case, you do not need to free the pinned pointer.

+7
source

It's simple!

Since you are using managed C ++, use include and proceed as follows:

#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
}
+7
source
#include <string>
#include <iostream>
#include <atlbase.h>
#include <atlconv.h>
#include <vcclr.h>

using namespace System;

int main(array<System::String ^> ^args)
{
    String^ managedStr = gcnew String(L"Hello, Managed string!");
    //If you want to convert to wide string
    pin_ptr<const wchar_t> wch = PtrToStringChars(managedStr);
    std::wstring nativeWstr(wch);
    //if you want to convert to std::string without manual resource cleaning
    std::string nativeStr(CW2A(nativeWstr.c_str()));
    std::cout<<nativeStr<<std::endl;
    Console::WriteLine(L"Hello World");
    return 0;
}
+3

jdehaan. , "" System:: String.

void MarshalNetToStdString(System::String^ s, std::string& os)
{
    using System::IntPtr;
    using System::Runtime::InteropServices::Marshal;

    const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer( );
    os = chars;
    Marshal::FreeHGlobal(IntPtr((void*)chars));
}

, System: String → std: string.

+1

CString, .

CString,

:

CString(Textbox->Text)

In your particular case:

private: System::Void Button_Click(System::Object^ sender, System::EventArgs^ e) 
{
    ofstream myfile (CString(Textbox->Text) + ".txt"); 
    myfile.close(); 
}
0
source

MSDN has a really great article on string conversions:

http://msdn.microsoft.com/en-us/library/ms235631%28vs.80%29.aspx

There are many examples for converting String from and to different types.

0
source

All Articles