Sign in to follow this  

Checking command line string args

This topic is 4665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a commandline application which can accept "/showampm" as a first argument, and I'm using the following two lines of code (well, there is more to it but this is the part that isnt working as expected) will check if the string exists in the first argument. But why does it not work? it just never get triggered!
char Cmd_ShowAmPm[] = "/showampm";
if (argv[1] == Cmd_ShowAmPm)
{
  //...
}

Share this post


Link to post
Share on other sites
You need to use a string character comparing function when using C-styled strings.

char Cmd_ShowAmPm[] = "/showampm";
if ( strcmp( argv[1], Cmd_ShowAmPm) == 0)
{
//...
}


Otherwise you are comparing addresses of the strings.

- Drew

Share this post


Link to post
Share on other sites
If you are using C++ you can also consider reading the command line into a vector of strings, which are more convenient and safer to use:


#include <string>
#include <iostream>
#include <vector>
std::vector<std::string> params;

int main(int argc, char* argv[])
{
// Read parameters
for (int count=1; count<argc; count++)
params.push_back(argv[count]);

if (params.size()>0 && params[0]=="/showampm") // do stuff
return 0;
}

Share this post


Link to post
Share on other sites

This topic is 4665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this