Jump to content
  • Advertisement
Sign in to follow this  
xyuri

Checking command line string args

This topic is 4972 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
Advertisement
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
I'm sticking to C style Char arrays for the time being :-) Thank you all for the help :-) I appreciate it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!