开发者

wchar to char in c++

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 narr开发者_如何学JAVAowing 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


It should be:

if (_tcscmp(argv[1], _T("-s")) == 0)


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")


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")


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜