• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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