我正在为这个错误撕掉我的头发。
------ 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
由于
科拉姆
答案 0 :(得分:1)
如果factoryname->Text
标识了一个属性,那么它的getter可能会抛出一个CLR异常,在这种情况下,你将泄漏文件句柄OutFile
。考虑不使用fopen
等。这是一个C库函数,有更好的选择,例如std::ofstream
。
或者,正如您可以使用.NET一样,您可以使用StreamWriter
来传递System::String
,从而避免转换问题:
StreamWriter writer(someFilePath);
writer.WriteLine(someString);
C ++ / CLI将在退出范围时关闭writer
。
答案 1 :(得分:0)
这只是转换错误:
cannot convert parameter 2 from 'System::String ^' to 'const char *'
可能的解决方案发布在这里:
What is the best way to convert between char* and System::String in C++/CLI