CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Feb 2007
    Posts
    112

    Simple question regarding command line argument

    Hi All,

    I am trying to run my code based on the command line argument.
    Following is the partial code:
    Code:
    int main(int argc, char *argv[]){
      std::cout<<"******************************"<<std::endl;
      std::cout<<std::endl;
      std::cout<<"Method is: "<<argv[1]<<std::endl; // prints command line argument
      .........
    ..............
    ...................
    }
    I want to check if the user has mentioned any command line argument or not, one simple way is to check "argc".....but I just want to know how do we check it through "argv[1]". For example, if i write the following code:
    Code:
    if(argv[1]==''){ //empty character
        std::cerr<<"Method Missing"<<std::endl;
        exit(1);
      }
    It doesn't work, as compiler says empty character constant.
    If I use the following:
    Code:
    if(argv[1]==""){ //string literal
        std::cerr<<"Method Missing"<<std::endl;
        exit(1);
      }
    Then the compiler warns me of: comparison with string literal results in unspecified behavior

    Can you guys suggest me, what is the best way.

    Thanks.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple question regarding command line argument

    It is meaningless to index argv at or beyond argc, so first check that.

    Second, in this case it's pointless to check for the empty string here because argv is built from all *non*-empty strings on the command-line.

    What you have to do is put in a particular option, like "-n", and check for that directly. If you get something else, your command line is corrupted and you throw an error.

  3. #3
    Join Date
    Feb 2007
    Posts
    112

    Re: Simple question regarding command line argument

    Thanks.
    You are right, its meaningless to to check argv beyond argc, as its not populated yet.
    Now I am using the following code:
    Code:
     if(argc<2){
        std::cerr<<"Method Missing"<<std::endl;
        exit(1);
      }
      else if(argv[1]!="old" && argv[1]!="new"){
        std::cerr<<"Not an appropriate Method, use - old or new"<<std::endl;
        exit(1);
        
      }
    The compiler warns me that comparison with string literals is not safe. Is there any other way to do this comparison?
    I want to make sure that the command line argument is either "new" or "old".

    Thanks.

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Simple question regarding command line argument

    'argv' is an array of pointers to a character that is (usually) an array of characters. You cannot compare a pointer to a string literal, you need to use functions like strcmp to compare them.
    Viggy

  5. #5
    Join Date
    Feb 2007
    Posts
    112

    Re: Simple question regarding command line argument

    Quote Originally Posted by MrViggy
    'argv' is an array of pointers to a character that is (usually) an array of characters. You cannot compare a pointer to a string literal, you need to use functions like strcmp to compare them.
    Viggy
    Thanks.
    After using std::strcmp, the compiler doesn't give me warning messages. But still, the code is not doing what I am expecting it to do. Following is the new code:

    Code:
    int main(int argc, char *argv[]){
      std::cout<<"argc:  "<<argc<<"   "<<"argv"<<"   "<<argv[1]<<std::endl;
      if(argc<2){
        std::cerr<<"Method Missing"<<std::endl;
        exit(1);
      }
      if(!std::strcmp(argv[1],"old") && !std::strcmp(argv[1],"new")){
        std::cerr<<"Not an appropriate Method, use - old or new"<<std::endl;
        exit(1);
        
      }
    
      std::cout<<"******************************"<<std::endl;
      std::cout<<std::endl;
      std::cout<<"Method is: "<<argv[1]<<std::endl
    ......................
    ..............
    }
    When I provide no argument, code is doing the right thing (printing out " method missing"). When I provide any other argument (old, new, ggg, jjj etc.), it NEVER go into the second "if" condition. Can anybody please lemme know where am I wrong?

    Thnks

  6. #6
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Simple question regarding command line argument

    Quote Originally Posted by pipa
    Thanks.
    After using std::strcmp, the compiler doesn't give me warning messages. But still, the code is not doing what I am expecting it to do. Following is the new code:

    Code:
    int main(int argc, char *argv[]){
      std::cout<<"argc:  "<<argc<<"   "<<"argv"<<"   "<<argv[1]<<std::endl;
      if(argc<2){
        std::cerr<<"Method Missing"<<std::endl;
        exit(1);
      }
      if(!std::strcmp(argv[1],"old") && !std::strcmp(argv[1],"new")){
        std::cerr<<"Not an appropriate Method, use - old or new"<<std::endl;
        exit(1);
        
      }
    
      std::cout<<"******************************"<<std::endl;
      std::cout<<std::endl;
      std::cout<<"Method is: "<<argv[1]<<std::endl
    ......................
    ..............
    }
    When I provide no argument, code is doing the right thing (printing out " method missing"). When I provide any other argument (old, new, ggg, jjj etc.), it NEVER go into the second "if" condition. Can anybody please lemme know where am I wrong?

    Thnks
    Did you read the reference that MrViggy linked to? strcmp returns 0 if the strings are equal, and zero of course converts to boolean false. Try:

    Code:
      if(std::strcmp(argv[1],"old") != 0 && std::strcmp(argv[1],"new")  != 0){
        std::cerr<<"Not an appropriate Method, use - old or new"<<std::endl;
        exit(1);
      }
    Also, don't use the exit function to end your program. Since you're in main anyway, just use a return statement.
    - Alon

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simple question regarding command line argument

    FYI....

    Code:
    int x;
    
    (x) == (x != 0);
    (!x) == (x == 0);
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Feb 2007
    Posts
    112

    Re: Simple question regarding command line argument

    Quote Originally Posted by TheCPUWizard
    FYI....

    Code:
    int x;
    
    (x) == (x != 0);
    (!x) == (x == 0);
    I am sorry, I may be slow. I didn't get what u r trying to say in the above.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simple question regarding command line argument

    Quote Originally Posted by pipa
    I am sorry, I may be slow. I didn't get what u r trying to say in the above.

    Comparing to X is the exact same as comparing to (X != 0).
    Comparint to !X is the exact same as comparing to (X==0).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Simple question regarding command line argument

    Quote Originally Posted by TheCPUWizard
    FYI....

    Code:
    int x;
    
    (x) == (x != 0);
    (!x) == (x == 0);
    Goes without saying. In this case, I much prefer making an explicit comparison to 0. This allows you to use comparison operators in a way that directly reflects the way we think about string comparison, for example:

    Code:
    strcmp(str1, str2) == 0; // str1 is equal to str2
    strcmp(str1, str2) != 0; // str1 does not equal str2
    strcmp(str1, str2) < 0; // str1 is "less than" str2
    strcmp(str1, str2) > 0; // str1 is "greater than" str2
    strcmp(str1, str2) <= 0; // etc.
    strcmp(str1, str2) >= 0; // etc.
    - Alon

  11. #11
    Join Date
    Feb 2007
    Posts
    112

    Re: Simple question regarding command line argument

    Quote Originally Posted by Hermit
    Did you read the reference that MrViggy linked to? strcmp returns 0 if the strings are equal, and zero of course converts to boolean false. Try:

    Code:
      if(std::strcmp(argv[1],"old") != 0 && std::strcmp(argv[1],"new")  != 0){
        std::cerr<<"Not an appropriate Method, use - old or new"<<std::endl;
        exit(1);
      }
    Also, don't use the exit function to end your program. Since you're in main anyway, just use a return statement.
    Thanks for ur suggestions. It worked!!
    I also changed exit to return. If we are in functions other than "main", should we always use exit? OR where should we use exit and where should we use return?

    Thanks.

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simple question regarding command line argument

    Quote Originally Posted by Hermit
    Goes without saying.
    For experienced people...yes...see the OP's reply.....

    In this case, I much prefer making an explicit comparison to 0.
    In many cases I do also....

    Code:
    if (strcmp(b,c))
    {
       if (strcmp(a,b)/strcmp(b,c) > 0) { .... } 
    }
    (That actually does something useful.....)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    Join Date
    Feb 2007
    Posts
    112

    Re: Simple question regarding command line argument

    Quote Originally Posted by TheCPUWizard
    Comparing to X is the exact same as comparing to (X != 0).
    Comparint to !X is the exact same as comparing to (X==0).
    Ok, I got it.
    You were talking in reference to my use of ( !strcmp(X) ).
    Thanks for explaining.

  14. #14
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Simple question regarding command line argument

    Quote Originally Posted by pipa
    Thanks for ur suggestions. It worked!!
    I also changed exit to return. If we are in functions other than "main", should we always use exit? OR where should we use exit and where should we use return?

    Thanks.
    Under normal circumstances, functions will complete whatever work they have to do and return to the calling function, eventually leading back to main. This gets a bit more complicated in an event-based application, where you need to post a quit message to stop the event loop, but it is fundamentally the same.

    If a function in your programs needs to abruptly stop what it's doing due to some exceptional circumstance, there is an aptly named C++ feature for that purpose: exceptions. There's a lot to know about exceptions and exception-safety, but for the purpose of answering your question, it suffices to say that this feature exists.

    Using the exit function is a lazy way to stop a program in the absence of proper program structure.
    - Alon

  15. #15
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Simple question regarding command line argument

    Quote Originally Posted by TheCPUWizard
    In many cases I do also....

    Code:
    if (strcmp(b,c))
    {
       if (strcmp(a,b)/strcmp(b,c) > 0) { .... } 
    }
    (That actually does something useful.....)
    I'm a little curious about this. Aren't the non-zero return values of strcmp unspecified? I can see how if you used multiplication it would be a sort of logical and (edit: only sort of), but division seems like it could round off to zero (imagine a strcmp implementation that returns the difference of the unequal characters).
    Last edited by Hermit; June 2nd, 2008 at 09:16 PM.
    - Alon

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured