#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;
}
Я бы использовал Marshalling:
//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);
, но обратите внимание, что вы тогда используете только строки ANSI. Если вам нужна поддержка Unicode, вы можете использовать WideChar STL-класс
и PtrtoStringchars
( #include
) для преобразования из системы: : Строка
. В таком случае вам не нужно освободить запись закрепленного.
Вы можете преобразовать его в CString, а затем добавить расширение к нему.
Существует встроенный конструктор CString, который позволит этому преобразованию произойти
пример:
CString(Textbox->Text)
в вашем конкретном случае:
private: System::Void Button_Click(System::Object^ sender, System::EventArgs^ e)
{
ofstream myfile (CString(Textbox->Text) + ".txt");
myfile.close();
}