0

everyone. I know there are a lot of related threads, but I can't understand them very well, so I decided to write my own.

I am trying to write a Win32 Console Application, and this is I would like to do:

Let's suppose my name app is: MyApp.exe, so I want every time I type in the command line:

MyApp.exe -W Hello

My app writes "Hello" in the output. Same as other arguments. Basically, I want to control every argument that I want but I don't know how to do that.

This is all I have:

    #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

int main(int argc, char *argv [])
{

    int count;


    printf("This program was called with  \"%s\". \n", argv[1]);
    printf("\n");

    system("Pause");

}

I mean, I know every argument is in the argv array, but I don't know how to parse that, like:

if(argv[1] == "-W")

It does not work.

Thanks a lot!

Sergio Calderon
  • 837
  • 1
  • 13
  • 32
  • Assuming you forgot the "C" language tag on this question, tag on this, take a look at [this possible duplicate](http://stackoverflow.com/questions/9642732/parsing-command-line-arguments) – WhozCraig Aug 12 '13 at 14:49

2 Answers2

0

You cannot do string compares using ==, you need something like

if (strcmp(argv[1], "-W") == 0)

For case-insensitive comparisons, you need to use _stricmp() instead.

See this MSDN article on String Manipulation.

Edward Clements
  • 5,040
  • 2
  • 21
  • 27
0

If you're using C, use the strcmp function:

if(strcmp(argv[1], "-W") == 0) { /* the first argument is -W */ }

If you're using C++, use the operator==:

if(std::string(argv[1]) == "-W") { /* the first argument is -W */ }
Smi
  • 13,850
  • 9
  • 56
  • 64