This is C++
At the windows cmd line user types
p3.exe X <data.txt
where "p3.exe" is the program name,
"X" will be a 1, 2, or 3,
and "data.txt" is some text file program uses for input.
Inside the main method, I'm expecting argv[1] to equal the string X typed at the cmd line. In fact, if I do
wcout << argv[1]
the output is "X" as expected.
So now I do this,
int main(int argc, char* argv[])
{
if (argc > 1)
{
if (argv[1] == "X")
{
//do stuff
}
}
return 0;
} // end main
But (argv[1] == "X") never evaluates to true
What am i missing or not understanding?
You can't compare C-style strings (char *
) with ==
because it only compares the pointer and not the pointed-to string.
You can either use strcmp
:
if (strcmp(argv[1], "X") == 0)
or make sure at least one side of the comparison is a C++ string:
if (std::string(argv[1]) == "X")