将 System::String 转换为 Const Char *

发布于 2024-08-18 00:26:23 字数 517 浏览 5 评论 0原文

我正在使用 Visual C++ 2008 的 GUI 创建器来制作用户界面。单击按钮时,将调用以下函数。内容应该创建一个文件,并以文本框“Textbox”的内容命名该文件,末尾带有“.txt”。但是,这导致我出现转换错误。以下是代码:

私有: System::Void Button_Click(System::Object^ 发送者,System::EventArgs^ e) { ofstream myfile(文本框->文本+“.txt”); myfile.close(); } 这是错误

错误 C2664: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const char *,std::ios_base::openmode,int)' : 无法将参数 1 从 'System::String ^' 转换为 '常量字符 *'

我怎样才能进行转换以允许它通过?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜还是长夜 2024-08-25 00:26:23

很简单!

当您使用托管 C++ 时,请使用 include 并进行如下操作:

#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
}

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
}
坐在坟头思考人生 2024-08-25 00:26:23

我会使用编组:

//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 类 wofstreamPtrToStringChars (#include) 从 <代码>系统::字符串。在这种情况下,您不需要释放固定的指针。

I would use 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);

But note that you then use just Ansi strings. If you need unicode support you can use the widechar STL class wofstream and PtrToStringChars (#include <vcclr.h>) to convert from System::String. In that case you do not need to free the pinned pointer.

不语却知心 2024-08-25 00:26:23
#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;
}
#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;
}
痴梦一场 2024-08-25 00:26:23

谢谢杰德汉。我几乎没有修改代码以将其与我的“正常”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 -> 这是这样的方式标准:字符串。

Thanks jdehaan. I little modified the code to use it with my 'normal' System::String's.

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));
}

This is the way if you want to convert System:String -> std:string.

另类 2024-08-25 00:26:23

您可以将其转换为 CString,然后向其添加扩展名。

有一个内置的 CString 构造函数将允许进行此转换

示例:

CString(Textbox->Text)

在您的具体情况下:

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

You can convert it to a CString and then add the extension to it.

There is a built-in CString constructor which will allow this conversion to happen

Example:

CString(Textbox->Text)

In your specific case:

private: System::Void Button_Click(System::Object^ sender, System::EventArgs^ e) 
{
    ofstream myfile (CString(Textbox->Text) + ".txt"); 
    myfile.close(); 
}
南…巷孤猫 2024-08-25 00:26:23

MSDN 中有一篇关于字符串转换的非常优秀的文章:

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

有许多示例可以将 String 与不同类型相互转换。

There is an really excellent article in MSDN about String conversions here:

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

There are many samples to convert String from and to different types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文