C++ C++;std::使用argv的for循环中的逻辑_错误
我是C++新手,我正在尝试编写一个程序来接受命令行参数并生成一个.desktop文件。我试图实现argv值的标识,但我一直得到一个std::logic\u错误 我的代码是:C++ C++;std::使用argv的for循环中的逻辑_错误,c++,C++,我是C++新手,我正在尝试编写一个程序来接受命令行参数并生成一个.desktop文件。我试图实现argv值的标识,但我一直得到一个std::logic\u错误 我的代码是: #include <iostream> #include <stdlib.h> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) {
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string name;
string comment;
for(int i = 1; i <= argc; i++) {
char* tmp[] = {argv[i]};
string param = *tmp;
string paramVal = argv[i+1];
if(param == "-h") {
cout << "-h Display this help dialogue" << endl;
cout << "-n Set entry name" << endl;
cout << "-c Set entry comment" << endl;
cout << "-e Set entry executable path" << endl;
cout << "-i Set entry icon" << endl;
break;
}
else if(param == "-n") {
name = paramVal;
i++;
continue;
}
else if(param == "-c") {
comment = paramVal;
i++;
continue;
}
else if(param == "-e") {
}
else if(param == "-i") {
}
else {
cout << "ERROR >>> Unrecognised parameter %s" << param << endl;
}
}
cout << "Name: %s\nComment: %s" << name << comment << endl;
return(0);
}
请帮助,因为这非常令人沮丧以下是我看到的问题:
i <= argc
这也将访问超出边界的数组
您可能想看看如何为您完成所有这些。投票结束是一个打字错误。你犯了一个错误<代码>我喜欢他们说的,编程中有三个大问题:命名问题和一个错误。为什么要绕道
tmp
而不是string param=argv[i]代码>?这是试图解决问题吗?我相信argc[argc]
没有通过数组,但它将是null ptr(作为哨兵令牌),绝对不应该取消引用。
i <= argc
string paramVal = argv[i+1];