在 C++ 中使用 fprintf代码

发布于 2024-11-30 09:17:09 字数 1300 浏览 1 评论 0原文

我有一个奇怪的问题。当我尝试编译下面的代码时,它按预期正常工作,没有失败:

#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 技术交流群。

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

发布评论

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

评论(2

じее 2024-12-07 09:17:09

错误很简单:VC++ 中 fprintf.c 中的第 55 行是 _VALIDATE_RETURN( (str != NULL), EINVAL, -1); 并且 strFILE* 参数(不过我见过更好的命名变量)。

对于好奇的人(我是)_VALIDATE_RETURN 定义如下:

#define _VALIDATE_RETURN( expr, errorcode, retexpr )                           \
    {                                                                          \
        int _Expr_val=!!(expr);                                                \
        _ASSERT_EXPR( ( _Expr_val ), _CRT_WIDE(#expr) );                       \
        if ( !( _Expr_val ) )                                                  \
        {                                                                      \
            errno = errorcode;                                                 \
            _INVALID_PARAMETER(_CRT_WIDE(#expr) );                             \
            return ( retexpr );                                                \
        }                                                                      \
    }

因此,在尝试写入不存在的文件描述符之前,最好检查您的 fopen() 调用是否成功。

The error is simple: Line 55 in fprintf.c in VC++ is _VALIDATE_RETURN( (str != NULL), EINVAL, -1); and str is the FILE* parameter (I've seen better named variables though).

For the curious (I was) _VALIDATE_RETURN is defined as follows:

#define _VALIDATE_RETURN( expr, errorcode, retexpr )                           \
    {                                                                          \
        int _Expr_val=!!(expr);                                                \
        _ASSERT_EXPR( ( _Expr_val ), _CRT_WIDE(#expr) );                       \
        if ( !( _Expr_val ) )                                                  \
        {                                                                      \
            errno = errorcode;                                                 \
            _INVALID_PARAMETER(_CRT_WIDE(#expr) );                             \
            return ( retexpr );                                                \
        }                                                                      \
    }

So better check if your fopen() call succeeds before trying to write to a nonexistant filedescriptor.

べ繥欢鉨o。 2024-12-07 09:17:09

好的,我找到了解决方案。唯一的问题是,如果您不在程序中输入完整路径,则文件不会打开。我换了

FILE *f = fopen("trystl.geo","w");

FILE *f = fopen("c:/Users/anypath/trystl.geo","w");

有效!

感谢您的帮助!

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

FILE *f = fopen("trystl.geo","w");

with

FILE *f = fopen("c:/Users/anypath/trystl.geo","w");

it works!

Thank you for all your help!

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