在 C++ 中使用 fprintf代码
我有一个奇怪的问题。当我尝试编译下面的代码时,它按预期正常工作,没有失败:
#include <iostream>
#include <Windows.h>
int main(){
FILE *f = fopen("trystl.geo","w");
fprintf(f,"Merge \"trystl.stl\";");
fprintf(f,"\n");
fprintf(f,"Surface Loop(2) = {1};");
fprintf(f,"\n");
fprintf(f,"Volume(3) = {2};");
fclose(f);
return 0;
}
但是当我尝试将此程序连接到具有 FLTK 用户界面的按钮时,它给了我一个断言运行时错误。我的代码段:
void UserInterface::cb_m_BtnSTLToGEOConverter_i(Fl_Button*, void*){
//OnSTLToGEOConvert();
FILE *f = fopen("trystl.geo","w");
fprintf(f,"Merge \"trystl.stl\";");
fprintf(f,"\n");
fprintf(f,"Surface Loop(2) = {1};");
fprintf(f,"\n");
fprintf(f,"Volume(3) = {2};");
fclose(f);
}
void UserInterface::cb_m_BtnSTLToGEOConverter(Fl_Button* o, void* v){
((UserInterface*)(o->parent()->parent()->parent()->parent()->parent()->parent()->parent()->user_data()))->cb_m_BtnSTLToGEOConverter_i(o,v);
}
当用户按下按钮时,我希望程序创建一个名为 trystl.geo 的文件并执行所示的操作。但是当编译并打开程序并单击按钮时,它会显示:
调试断言失败!
程序:*.......\src\fprintf.c 第 55 行:
表达式:(str!NULL)
中止重试或忽略...
我正在使用 Visual Studio 2010。
I have an odd problem. When I try to compile the code below, it works without a failure as expected:
#include <iostream>
#include <Windows.h>
int main(){
FILE *f = fopen("trystl.geo","w");
fprintf(f,"Merge \"trystl.stl\";");
fprintf(f,"\n");
fprintf(f,"Surface Loop(2) = {1};");
fprintf(f,"\n");
fprintf(f,"Volume(3) = {2};");
fclose(f);
return 0;
}
But when I try to connect this program to a button with FLTK user interface, it gives me an assertion runtime error. The segment of my code:
void UserInterface::cb_m_BtnSTLToGEOConverter_i(Fl_Button*, void*){
//OnSTLToGEOConvert();
FILE *f = fopen("trystl.geo","w");
fprintf(f,"Merge \"trystl.stl\";");
fprintf(f,"\n");
fprintf(f,"Surface Loop(2) = {1};");
fprintf(f,"\n");
fprintf(f,"Volume(3) = {2};");
fclose(f);
}
void UserInterface::cb_m_BtnSTLToGEOConverter(Fl_Button* o, void* v){
((UserInterface*)(o->parent()->parent()->parent()->parent()->parent()->parent()->parent()->user_data()))->cb_m_BtnSTLToGEOConverter_i(o,v);
}
When the user presses the button, I want the program to create a file called trystl.geo and perform the operations shown. But when compile and open the program and click the button, it says:
Debug Assertion Failed!
Program: *.......\src\fprintf.c Line 55:
Expression: (str! NULL)
abort retry or ignore...
I'm using Visual Studio 2010.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误很简单:VC++ 中 fprintf.c 中的第 55 行是
_VALIDATE_RETURN( (str != NULL), EINVAL, -1);
并且str
是FILE*
参数(不过我见过更好的命名变量)。对于好奇的人(我是)
_VALIDATE_RETURN
定义如下:因此,在尝试写入不存在的文件描述符之前,最好检查您的 fopen() 调用是否成功。
The error is simple: Line 55 in fprintf.c in VC++ is
_VALIDATE_RETURN( (str != NULL), EINVAL, -1);
andstr
is theFILE*
parameter (I've seen better named variables though).For the curious (I was)
_VALIDATE_RETURN
is defined as follows:So better check if your fopen() call succeeds before trying to write to a nonexistant filedescriptor.
好的,我找到了解决方案。唯一的问题是,如果您不在程序中输入完整路径,则文件不会打开。我换了
它
有效!
感谢您的帮助!
OK I found the solution. The only problem is that if you don't type in the whole path in the program, the file doesn't get opened. I replaced
with
it works!
Thank you for all your help!