Compare _TCHAR* argv[] entries from command line to _T("paramString")
I know how to get the parameters from the command line. I also know how to print them out.
The problem I'm having is how to compare the parameters from the argv[] array to a string. The progam runs but never returns a result where the parameter string is equal to the one I'm looking for.
Thanks in advance.
// Testing.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < argc; i = i + 1)
{
if (argv[i] == _T("find"))
{
wcout << "found at position " << i << endl;
}
else
{
wcout << "not found at " << i << endl;
开发者_高级运维 }
}
return 0;
}
As the other answers say, strcmp()
or wsccmp()
depending on whether you are compiling with UNICODE defined, which _tcscmp()
from will do for you.
You need to use strcmp function for the compare. what you're doing write now is just comparing the pointers. please note that strcmp returns 0 if the strings are equal.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < argc; i = i + 1)
{
if (_tcscmp(argv[i], _T("find")==0)
{
wcout << "found at position " << i << endl;
}
else
{
wcout << "not found at " << i << endl;
}
}
Simon wrote:
which _tccmp() from will do for you
This is actually wrong.
_tccmp()
compares chars (so it would only compare the 'f' in "find").
It's _tcscmp()
doing the job!
if (argv[i] == _T("find"))
This will only the compare the pointers argv[i]
and pointer pointed to "find".
What you need is to compare the strings. you can use strcmp
, ( wcscmp
, for unicode)
0 == wcscmp( argv[i], _T("find"))
精彩评论