尝试将文本字段值写入 c++ 中的文件

发布于 2024-09-08 05:27:08 字数 1518 浏览 2 评论 0原文

我对这个错误感到抓狂。

 ------ Build started: Project: shotfactorybatchgen, Configuration: Debug Win32 ------
  shotfactorybatchgen.cpp
c:\documents and settings\administrator\my documents\visual studio 2010\projects\shotfactorybatchgen\shotfactorybatchgen\Form1.h(307): error C2664: 'fprintf' : cannot convert parameter 2 from 'System::String ^' to 'const char *'
          No user-defined-conversion operator available, or
          Cannot convert a managed type to an unmanaged type
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我浏览了整个互联网,但找不到答案。这是发生错误的代码。

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
   Decimal value;
   if(!Decimal::TryParse(textBox4->Text, value)) {
    MessageBox::Show("Non-numeric characters detected in 'Wait Time' filed", "Oops", MessageBoxButtons::OK, MessageBoxIcon::Warning);
   } else {
    if(!Decimal::TryParse(textBox3->Text, value)) {
     MessageBox::Show("Non-numeric characters detected in 'Max Upload' filed", "Oops", MessageBoxButtons::OK, MessageBoxIcon::Warning);
    } else {
     FILE *OutFile = fopen("run.bat","w");
     fprintf(OutFile,"@ECHO OFF\r\nC:\\Python26\\python.exe factory.py");
     if(factoryname->Text != ""){
      fprintf(OutFile," -f ");
      fprintf(OutFile,factoryname->Text);
     }
     fclose(OutFile); 
    }
   }
   }

有什么想法吗?这是一个简单的 Windows 窗体应用程序。我正在使用 Visual Studio C++ 2010

谢谢

专栏

I am tearing my hair out over this error.

 ------ Build started: Project: shotfactorybatchgen, Configuration: Debug Win32 ------
  shotfactorybatchgen.cpp
c:\documents and settings\administrator\my documents\visual studio 2010\projects\shotfactorybatchgen\shotfactorybatchgen\Form1.h(307): error C2664: 'fprintf' : cannot convert parameter 2 from 'System::String ^' to 'const char *'
          No user-defined-conversion operator available, or
          Cannot convert a managed type to an unmanaged type
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I have looked all around the interwebs but I can not find an answer. Here is the code that the error is happening in.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
   Decimal value;
   if(!Decimal::TryParse(textBox4->Text, value)) {
    MessageBox::Show("Non-numeric characters detected in 'Wait Time' filed", "Oops", MessageBoxButtons::OK, MessageBoxIcon::Warning);
   } else {
    if(!Decimal::TryParse(textBox3->Text, value)) {
     MessageBox::Show("Non-numeric characters detected in 'Max Upload' filed", "Oops", MessageBoxButtons::OK, MessageBoxIcon::Warning);
    } else {
     FILE *OutFile = fopen("run.bat","w");
     fprintf(OutFile,"@ECHO OFF\r\nC:\\Python26\\python.exe factory.py");
     if(factoryname->Text != ""){
      fprintf(OutFile," -f ");
      fprintf(OutFile,factoryname->Text);
     }
     fclose(OutFile); 
    }
   }
   }

Any ideas? It is a simple windows form application. I am using Visual Studio C++ 2010

Thanks

Colum

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

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

发布评论

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

评论(2

一世旳自豪 2024-09-15 05:27:08

如果 factoryname->Text 标识了一个属性,那么它的 getter 可能会抛出 CLR 异常,在这种情况下,您将泄漏文件句柄 OutFile。考虑不使用fopen等。它是一个C库函数,并且有更好的替代品,例如std::ofstream

或者,由于您有 .NET 可用,您可以使用 StreamWriter ,它可以让您传递 System::String ,从而也避免您的转换问题:

StreamWriter writer(someFilePath);
writer.WriteLine(someString);

C++/CLI 将采用当范围退出时,请注意关闭writer

If factoryname->Text identifies a property, then its getter may throw a CLR exception, in which case you will leak the file handle OutFile. Consider not using fopen, etc. It's a C library function and there are better alternatives such as std::ofstream.

Alternatively, as you have .NET available, you could use StreamWriter which will let you pass System::String and thus avoid your conversion problem as well:

StreamWriter writer(someFilePath);
writer.WriteLine(someString);

C++/CLI will take care of closing the writer when the scope is exited.

一个人的旅程 2024-09-15 05:27:08

这只是一个转换错误:

cannot convert parameter 2 from 'System::String ^' to 'const char *'

此处发布了可能的解决方案:

在 C++/CLI 中 char* 和 System::String 之间转换的最佳方式是什么

打开文件

将 System::String 转换为 const char*

It's simply a conversion error:

cannot convert parameter 2 from 'System::String ^' to 'const char *'

Possible solution is posted here:

What is the best way to convert between char* and System::String in C++/CLI

Opening Files

Convert System::String to const char*

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