• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Finding arguments

Why dosen't the following code find the "/s:" argument ?

When I pass it as an agument of "/s:"

MyApp  /s

Thanks

int main(int argc, char* argv[])
{
	for (int i = 1; i < argc; i++)
	{
		//printf(argv[i]); printf("\n");
		printf( "arg %d: '%s'\n", i, argv[i] );

		if (argv[i] == "/s:")
		{
			printf("Found It...\n");
		}
	}
	
	return 0;
}

Open in new window

0
sidwelle
Asked:
sidwelle
  • 2
  • 2
2 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
You need to use strcmp, currently you are just comparing pointers not the strings they point to.
0
 
farzanjCommented:
You can manually do it or use getopt library that makes it a whole lot easier

http://www.codeproject.com/KB/cpp/xgetopt.aspx
0
 
sidwelleAuthor Commented:
My if statment now looks like:
      if (strcmp(argv, "/s:")==0)
      {
            printf("Found It...\n");
      }

But what If I only want to compare the first 3 characters ?
0
 
evilrixSenior Software Engineer (Avast)Commented:
Use strncmp instead.
0
 
sidwelleAuthor Commented:
Thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now