根据输入的选项/参数,您将如何使用 argv[] 的 if/then 语句?
例如,a.out -d 1 sample.txt 与 a.out -e 1 sample.txt。
int main (int argc, char *argv[])
{
ifstream infile(argv[3]);
int c;
int number = 0;
int count = 0;
while ( infile.good() ) {
if (argv[1] == "-d")
{
c = infile.get();
number = atoi(argv[2]);
if (number == count)
{
cout.put(c);
count = 0;
}
else
count++;
}
if (argv[1] == "-e")
{
cout << "entered -e" << endl; //testing code
}
}//end while
}//end main
最佳答案
你不能使用相等运算符来比较 C 风格的字符串,你必须使用 std::strcmp
:
if (std::strcmp(argv[1], "-d") == 0)
这背后的原因是 ==
运算符比较的是指针,而不是它们指向的内容。
关于c++ - 在 C++ 中使用 if/then 语句解析 argv[ ] 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13599915/