views:

214

answers:

4

I have a Windows CE console application that's entry point looks like this

int _tmain(int argc, _TCHAR* argv[])

I want to check the contents of argv[1] for "-s" convert argv[2] into an integer. I am having trouble narrowing the arguments or accessing them to test.

I initially tried the following with little success

if (argv[1] == L"-s")

I also tried using the narrow function of wostringstream on each character but this crashed the application.

Can anyone shed some light? Thanks

+6  A: 

It should be:

if (_tcscmp(argv[1], _T("-s")) == 0)
Matthew Flaschen
i think `wcscmp` is preffered
Andrey
I changed it to `_tcscmp`, which is the most flexible.
Matthew Flaschen
A: 

argv is an array of char*, not string, so the usual comparison with == won't work. That if-statement is comparing two pointers. Can you construct a wstring and then compare?

if (wstring(argv[1]) == L"-s")
Kristo
+2  A: 
if (argv[1] == L"-s")

This is not correct even for narrow strings as you compare pointers. You need

if(wcscmp(argv[1],L"-s")==0)

or

if(std::wstring(argv[1])==L"-s")
Artyom
A: 

May be you need to convert the argv[] entry to a string and then do the comparison. [link text][1]

http://www.codeproject.com/Tips/81778/Converting-TCHAR-to-string-while-getting-PC-Name.aspx

[1]:

The above link would help if you can do a conversion to string.

Wajih