1

So here I have

if(argv[0] == L"test") {
    cout << "it is test";
}
else {
    cout << "Nope."
}

And it always displays "Nope." I don't know what I'm doing wrong... I've tried using different ways but they all end up the same way.

myaut
  • 11,174
  • 2
  • 30
  • 62
Colby Ryan Freeman
  • 325
  • 1
  • 3
  • 8
  • Is the parameter being passed actually a wide character string? If it is, then maybe the environment that you are using to compile has made provisions to specially process command arguments given to `main` that are wide strings (for example Visual Studio has, I believe, `TCHAR**` for the argument array). – PaulMcKenzie Aug 25 '15 at 20:44
  • @PaulMcKenzie doesnt matter arv[0] == any sort of string isnt going to work. some form of strcmp is needed – pm100 Aug 25 '15 at 21:33
  • Yes, I know. Just wondering if the runtime actually is capable of sending wide string arguments or not. – PaulMcKenzie Aug 25 '15 at 21:40
  • @BoPersson this question touches on `wchar_t` issues whereas the other one doesn't, so I wonder if it should be reopened – M.M Aug 25 '15 at 23:50

3 Answers3

3

A more C++ like solution would be:

#include <string>
#include <iostream>

using namespace std;

int main(int argc, wchar_t* argv[])
{
    if (argv[0] == wstring(L"test")) {
        cout << "it is test";
    }
    else {
        cout << "Nope.";
    }

    return 0;
}
Moby Disk
  • 3,761
  • 1
  • 19
  • 38
  • not a fan personally - `wcscmp` exists and works correctly; constructing two `wstring` objects just wastes time and memory – M.M Aug 25 '15 at 23:46
1

You can use wcscmp().

You're main function has to pass a wide character for argv[0] like so:

int main(int argc, wchar_t*argv[])

then you can do:

 if (wcscmp(argv[0], L"test"))
pistachiobk
  • 304
  • 2
  • 15
0

You need to use strcmp as argv is an array of char*.

Your code compares the pointers instead of the string content.

antoyo
  • 11,097
  • 7
  • 51
  • 82
  • 1
    It's a bit more complicated when comparing `wchar_t` strings (`L"test"`) to `char` strings. Some combination of `mbstowcs()` and `wcscmp()` would be in order. – Paul Roub Aug 25 '15 at 20:27