Comparing command parameter with argv[] is not working

后端 未结 4 2048
温柔的废话
温柔的废话 2021-01-17 16:01

I am trying to compare the parameter of command with argv[] but it\'s not working. Here is my code.

./a.out -d 1

In main function



        
4条回答
  •  离开以前
    2021-01-17 16:50

    In C++ let std::string do the work for you:

    #include 
    int main (int argc, char * const argv[]) {
    
    if (argv[1] == std::string("-d"))
    
    // call some function here
    
    }
    

    In C you'll have to use strcmp:

    if (strcmp(argv[1], "-d") == 0)
    
    // call some function here
    
    }
    

提交回复
热议问题