【问题标题】:debug assertion error调试断言错误
【发布时间】:2009-06-04 08:06:32
【问题描述】:

我一直在摸不着头脑,当我第一次使用 cmd 进入 project\debug 文件夹然后在那里运行程序时,这段代码运行良好。然后我添加了 if(in) 和 else 部分,然后它开始给我“调试断言失败”错误 mbstowcs.c

表达式 s != NULL

这对我来说没有任何意义..

我在cmd中使用了这个命令:prog.exe test.txt nuther.txt

这两个文件都存在于调试文件夹和主项目文件夹中..

有什么想法吗?

    int main(int argc, char **argv)
        {
        parse_opts(argc, argv); //parse the arguments

        return 0;
    }


    void parse_opts(int argc, char **argv)
    {
        string compl_out;

        if( argc > 1 )
        {
            for( int i = 1; i < argc; i++ )
            {
                if( argv[i][0] = '>' )
                {
                    ofstream out_file(argv[i+1]);
                    out_file << compl_out;
                    out_file.close();
                    break;
                }

                ifstream in(argv[i]);
                string buff;

                if(in)
                {
                    while(getline( in, buff ))
                    cout << buff << endl;

                    compl_out.append(buff); 
                }
                else
                {
                    cout << "Can't open file: " << argv[i] 
                            << ", file doesn't exist or is locked in use. " << endl;
                }
            }
        }
        else
        {
            usage();
        }

}

【问题讨论】:

    标签: c++ debugging


    【解决方案1】:

    第一印象:

    if( argv[i][0] = '>' )
    

    应该是:

    if( argv[i][0] == '>' )
    

    你是在分配而不是比较。

    我想您也可能打算将 compl_out.append 放在 while 循环中?因为它不会向该缓冲区附加任何内容:

    while(getline( in, buff ))
    {
        cout << "buf" << buff << endl;
        compl_out.append(buff); 
    }
    

    【讨论】:

    • 是的,我在发布后立即看到了。哈哈,像这样的愚蠢错误太令人沮丧了..
    • 好东西......不知道为什么你会得到断言。除非我错过了什么,否则我希望程序在考虑到这些参数的情况下退出。虽然如果你通过“blah.exe >”它可能会在 ofstream out_file(argv[i+1]);行。
    • 一个常见的“技巧”是在右侧写常量表达式,如果你养成这样的习惯,可能会为你省去一些麻烦。例如。 if ( '>' == arg[i][0] ) 没问题, if ( '>' = arg[i][0] ) 会出现编译错误。
    • @Anders 这是一个很酷的技巧。我想知道我的虚拟肌肉记忆是否过于根深蒂固而无法采用这种做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多