Visual studio 2010 使用strcmp将argv项与字符串文本isn';I don’我工作得不像我期望的那样好 我对Visual C++非常陌生,所以这可能是一个“男生错误”,但是下面的代码没有按照我预期的那样执行: #include "stdafx.h" #include <string.h> int _tmain(int argc, _TCHAR* argv[]) { if (strcmp((char*)argv[1], "--help") == 0) { printf("This is the help message."); //Won't execute } return 0; }
我希望看到消息Visual studio 2010 使用strcmp将argv项与字符串文本isn';I don’我工作得不像我期望的那样好 我对Visual C++非常陌生,所以这可能是一个“男生错误”,但是下面的代码没有按照我预期的那样执行: #include "stdafx.h" #include <string.h> int _tmain(int argc, _TCHAR* argv[]) { if (strcmp((char*)argv[1], "--help") == 0) { printf("This is the help message."); //Won't execute } return 0; },visual-studio-2010,visual-c++,argv,strcmp,Visual Studio 2010,Visual C++,Argv,Strcmp,我希望看到消息这是帮助消息。但我没有看到它-调试显示if条件的结果是-1,而不是我期望的0。我做错了什么?好的,我已经弄明白发生了什么。argv[]数组被声明为TCHAR*,这是一个宏,根据是否为项目启用了Unicode来调整类型(wchat\t如果启用,或者char如果未启用)。我尝试使用的strcmp函数是非Unicode字符串比较,而wcscmp是Unicode等效函数。\u tcscmp函数根据Unicode设置使用适当的字符串比较函数。如果我用\u tcscmp替换strcmp,问题就
这是帮助消息。
但我没有看到它-调试显示if
条件的结果是-1,而不是我期望的0。我做错了什么?好的,我已经弄明白发生了什么。argv[]
数组被声明为TCHAR*
,这是一个宏,根据是否为项目启用了Unicode来调整类型(wchat\t
如果启用,或者char
如果未启用)。我尝试使用的strcmp
函数是非Unicode字符串比较,而wcscmp
是Unicode等效函数。\u tcscmp
函数根据Unicode设置使用适当的字符串比较函数。如果我用\u tcscmp
替换strcmp,问题就解决了
#include "stdafx.h"
#include <string.h>
int _tmain(int argc, _TCHAR* argv[])
{
if (_tcscmp(argv[1], _T("--help")) == 0)
{
printf("This is the help message."); //Will execute :)
}
return 0;
}
#包括“stdafx.h”
#包括
int _tmain(int argc,_TCHAR*argv[]
{
if(_tcscmp(argv[1],_T(“--help”)==0)
{
printf(“这是帮助消息”);//将执行:)
}
返回0;
}
如果启用了Unicode,则\u T
函数将参数转换为Unicode
另请参见:好的,我已经知道发生了什么。
argv[]
数组被声明为TCHAR*
,这是一个宏,根据是否为项目启用了Unicode来调整类型(wchat\t
如果启用,或者char
如果未启用)。我尝试使用的strcmp
函数是非Unicode字符串比较,而wcscmp
是Unicode等效函数。\u tcscmp
函数根据Unicode设置使用适当的字符串比较函数。如果我用\u tcscmp
替换strcmp,问题就解决了
#include "stdafx.h"
#include <string.h>
int _tmain(int argc, _TCHAR* argv[])
{
if (_tcscmp(argv[1], _T("--help")) == 0)
{
printf("This is the help message."); //Will execute :)
}
return 0;
}
#包括“stdafx.h”
#包括
int _tmain(int argc,_TCHAR*argv[]
{
if(_tcscmp(argv[1],_T(“--help”)==0)
{
printf(“这是帮助消息”);//将执行:)
}
返回0;
}
如果启用了Unicode,则\u T
函数将参数转换为Unicode
另见: