首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Return true是return false吗?魔术

Return true是return false吗?魔术
EN

Stack Overflow用户
提问于 2015-05-16 15:09:51
回答 1查看 964关注 0票数 3

我在从VoIP包中提取音频的C++程序中遇到问题。

它在Linux和OpenBSD上运行得很好,在amd64和x86上运行得很好,但是当我在ARM上的OpenBSD上运行程序时,它真的很神奇。

我在LoadConfigFile中调用return true,在ProcessConfiguration中调用result false。

有谁能帮帮我吗?也许我只是个真正的盲人。我在代码中做了很多测试。

这是调用函数ProcessConfiguration的程序的主要函数。

代码语言:javascript
复制
int main ( int argc, char **argv ) {

    if ( ! config.ProcessConfiguration( argc, argv ) ) {
        std::cout << "process configuration failed" << std::endl;
        std::cout << "exiting whole program with EXIT_FAILURE" << std::endl;
        return EXIT_FAILURE;
    }

    std::cout << "process configuration is true" << std::endl;

    // User want to only print configuration
    if ( config.m_printconfig == true ) {
        config.PrintConfig();
        return EXIT_SUCCESS;
    }
    .
    . // Long uninteresting code
    .
    return EXIT_SUCCESS;
}

这是调用LoadConfigFile的函数ProcessConfoguration。

代码语言:javascript
复制
bool TConfig::ProcessConfiguration ( int & argc, char ** & argv ) {

    // Scan options from command line
    int c;
    while (( c = getopt ( argc, argv, "c:dhn" )) != EOF ) {
        switch (c) {
        case 'c':
            m_configfile = optarg;
            break;
        case 'd':
            m_daemon = false;
            break;
        case 'h':
            m_printusage = true;
            return true;
        case 'n':
            m_printconfig = true;
            break;
        default:
            return false;
        }
    }
    argc -= optind;
    argv += optind;

    if ( LoadConfigFile() == false ) {
        std::cout << "LoadConfigFile was false" << std::endl;
        return false;
    }
    std::cout << "LoadConfigFile was true" << std::endl;
    return true;
}

此函数加载配置文件,解析所有指令。

代码语言:javascript
复制
bool TConfig::LoadConfigFile ( void ) {

    std::string line;
    std::string directive;
    std::ifstream data;

    if ( m_configfile.empty() ) {
        m_configfile = DEFAULT_CONFIGFILE;
    }

    std::cout << "opening file " << m_configfile << std::endl;

    data.open( m_configfile.c_str() );

    if ( ! data.is_open() ) {
        std::cerr << "Couldn't open config " << m_configfile << std::endl;
        return false;
    }
    std::cout << "configfile is open" << std::endl;
    std::cout << "before getline" << std::endl;
    while ( getline( data, line ) ) {
        std::cout << "after getline" << std::endl;
        trim_whitespaces( line );
        .
        . // Long uninteresting code
        .
    }
    std::cout << "before data.close" << std::endl;
    data.close();
    std::cout << "before return true in LoadConfigFile" << std::endl;
    return true;
}

这是在终端上输出的。

代码语言:javascript
复制
$ ./call-extract -c call-extract.conf -d -n 
opening file call-extract.conf
configfile is open
before getline
before data.close
before return true in LoadConfigFile
LoadConfigFile was false
process configuration failed
exiting whole program with EXIT_FAILURE
$ echo $?
1
$ 

我在LoadConfigFile中调用return true,在ProcessConfiguration中调用result false。

EN

回答 1

Stack Overflow用户

发布于 2015-09-18 22:40:17

返回的true为true。

我的输出:

代码语言:javascript
复制
before return true in LoadConfigFile
LoadConfigFile was true

没有无趣部分的代码:

代码语言:javascript
复制
struct TConfig
{
    bool ProcessConfiguration()
    {
        bool rv = true;
        if( !LoadConfigFile() ) { rv = false; }
        printf("LoadConfigFile was %s\n", rv?"true":"false" );
        return rv;
    }

    bool LoadConfigFile ( void )
    {
        printf("before return true in LoadConfigFile\n");
        return true;
    }
};
void fv()
{
    TConfig config; config.ProcessConfiguration();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30272858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档