简体   繁体   English

wchar在c ++中使用char

[英]wchar to char in c++

I have a Windows CE console application that's entry point looks like this 我有一个Windows CE控制台应用程序,其入口点如下所示

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

I want to check the contents of argv[1] for "-s" convert argv[2] into an integer. 我想检查argv[1]的内容为"-s"argv[2]转换为整数。 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. 我也尝试在每个字符上使用wostringstreamnarrow函数, wostringstream会使应用程序崩溃。

Can anyone shed some light? 任何人都能解释一下吗? Thanks 谢谢

它应该是:

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. argv是一个char*数组,而不是string ,所以与==的通常比较不起作用。 That if-statement is comparing two pointers. if语句比较两个指针。 Can you construct a wstring and then compare? 你能构建一个wstring然后比较吗?

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

May be you need to convert the argv[] entry to a string and then do the comparison. 可能是您需要将argv []条目转换为字符串然后进行比较。 [link text][1] [链接文字] [1]

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

[1]: [1]:

The above link would help if you can do a conversion to string. 如果您可以转换为字符串,上述链接将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM