Visual studio 2008 将命令行中的“TCHAR*argv[]项与“T”(“paramString”)进行比较
我知道如何从命令行获取参数。我也知道如何打印出来 我遇到的问题是如何将argv[]数组中的参数与字符串进行比较。程序运行,但从不返回参数字符串等于我要查找的字符串的结果 提前谢谢Visual studio 2008 将命令行中的“TCHAR*argv[]项与“T”(“paramString”)进行比较,visual-studio-2008,visual-c++,Visual Studio 2008,Visual C++,我知道如何从命令行获取参数。我也知道如何打印出来 我遇到的问题是如何将argv[]数组中的参数与字符串进行比较。程序运行,但从不返回参数字符串等于我要查找的字符串的结果 提前谢谢 // Testing.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; int _tmain(int ar
// 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;
}
//Testing.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
对于(int i=0;i wcout您需要使用strcmp函数进行比较。
你现在写的只是比较指针。
请注意,如果字符串相等,strcmp返回0
#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;
}
}
#包括“stdafx.h”
#包括
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
对于(int i=0;i wcout您需要使用strcmp函数进行比较。
你现在写的只是比较指针。
请注意,如果字符串相等,strcmp返回0
#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;
}
}
#包括“stdafx.h”
#包括
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
对于(int i=0;iargv[i]
和指向的指针“find”。
您需要的是比较字符串。您可以使用(wcscmp
,用于unicode)
这将只比较指针argv[i]
和指向的指针“find”。
您需要的是比较字符串。您可以使用(wcscmp
,用于unicode)
正如其他答案所说,strcmp()
或wsccmp()
取决于您是否使用UNICODE定义的代码进行编译,其中\u tcscmp()
将为您提供帮助。正如其他答案所说,strcmp()
或wsccmp()
取决于您是否使用UNICODE定义的代码进行编译,而\u tcscmp()from可以帮你。西蒙写道:
哪一个_tccmp()适合您
这其实是错误的。
\u tccmp()
比较字符(因此它只比较“find”中的“f”)
这是\u tcscmp()
做的工作!西蒙写道:
哪一个_tccmp()适合您
这其实是错误的。
\u tccmp()
比较字符(因此它只比较“find”中的“f”)
这是\u tcscmp()
做的工作!不!看@fseydel答案:\u tccmp()
只比较第一个字符或每个给定的指针。@Seki:谢谢!我更新了正确的宏,因为我不知怎么被接受了(3年前!)。\u tccmp()
甚至没有正确的文档记录,所以我可能只是键入了它,但令人害怕的是它有相同的签名。这只是为了帮助一些人找到这个答案,就像我昨天做的那样,他们会想知道为什么字符串比较如此奇怪…:^不!看看@fseydel answer:\u tccmp()
只比较第一个字符或每个给定的指针。@Seki:谢谢!我更新了正确的宏,因为我不知怎么被接受了(3年前!)。\u tccmp()
甚至没有正确的文档记录,所以我可能只是键入了它,但令人害怕的是它有相同的签名。这只是为了帮助一些人找到这个答案,就像我昨天做的那样,他们会想知道为什么字符串比较如此奇怪……:^)
0 == wcscmp( argv[i], _T("find"))