【问题标题】:Check command line arguments is not char c++检查命令行参数不是 char c++
【发布时间】:2019-10-30 16:45:39
【问题描述】:

我正在尝试检查命令行参数是字母还是数字

int main(int argc, char* argv[])
{
    if(argv[1] is not int) //How does it check if it is number or alphabets
    { 
        cerr<< "arguments must be integer" << endl;
        return -1;
    }
}

如果我在 linux 终端中运行,输出会是这样的

./a.out aasmsnsak
arguments must be integer

【问题讨论】:

  • 最简单的方法是将参数传递给strtolstd::stoi,看看是否可以将参数解析为整数。
  • 使用 std::isalpha

标签: c++ command-line-arguments


【解决方案1】:

使用标准库执行此操作的一种方法是使用来自&lt;cstdlib&gt;strtol。另一个使用istringstream

char *endptr;
const long int argument = strtol (argv[1], &endptr, 10);  // <cstdlib>
if(endptr[0] != '\0') {
    cerr << "arguments must be integer" << endl;
    return -1;
} else if (argument == LONG_MIN || argument == LONG_MAX) {  // <climits>
    // Alternatively you can check for ERANGE in errno
    cerr << "arguments value is out of range" << endl;
    return -1;
} else {
    /* Right type of argument received */
    [..]
}

Demo using strtol

istringstream iss(argv[1]);
int argument;
char more;
if(iss >> argument) {
    if(iss >> more) {
        cerr << p << ": arguments must be integer" << endl;
        return -1;
    } else {
        /* Right type of argument received */
        [..]
    }
} else {
    cerr << p << ": arguments must be integer" << endl;
    return -1;
}

Demo using istringstream

【讨论】:

  • 使用 try 和 catch 会在我脑海中浮现你认为它更好吗?喜欢使用 ??
  • 在你的 strtol 中,'\0' 是什么意思?
  • @GlenioAlsinTan '\0' 是空字符的字符代码。 C 中的字符串必须以 null 结尾;否则常见的字符串操作例程不知道何时停止处理。对于strtol,它会在发现任何不可能是数字的内容时停止解析。这可能是空终止符,也可能是其他任何不是数字的东西。 strtol 设置 endptr 指向停止解析的字符。如果它指向空终止符,则读取整个字符串。如果它指向其他任何东西,则整个字符串不是整数。
  • @user4581301 strtol(argv[1], &endptr, 10);常数 10 是什么意思?在cplusplus.com/reference/cstdlib/strtol 说它是一个基础,它有什么变化?
  • @GlenioAlsinTan 10 是数字的基数或基数。您可以将10 设置为十进制,2 设置为二进制,16 设置为十六进制等。如果使用值0,则表示自动从字符串中推断出基数。例如,如果字符串以0 开头,则该数字被解析为八进制,以0x 开头的数字被解析为十六进制,以非零数字开头的数字被解析为十进制。
【解决方案2】:

一开始,命令行参数可能包含多个字符。所以你必须遍历所有这些:

for(char* arg = argv[1]; *arg; ++arg)

现在我假设您想将包含 any 非数字字符的字符串分类为非数字,因此在循环内,您将检查每个字符的数字:

if(!isdigit(static_cast<unsigned char>(*arg)))
{
    // non-number-string!

    break; // no need to go on...
}

另一种变体可能只是尝试转换为数字:

std::istringstream s(argv[1]);
int n;
s >> n;
if(s) // number read?
{
    char c;
    s >> c;
    if(!s.eof())
    {
        // not last character reached!
        // -> no number
    }
}

或者:

char* end;
long number = strtol(argv[1], &end, 10);
//                              ^ needed for detecting, if end of string reached
if(*end || errno == ERANGE)
{
    // doesn't point to terminating null character -> no number
    // or number read was out of range
}

C 风格的变体:

int n, int l;
if(sscanf(argv[1], "%d%n", &n, &l) == 1 && argv[1][l] == 0)
{
    // successfully scanned an integer and end of string reached -> is a number
}

【讨论】:

  • 非常感谢,我已将我的问题编辑为( argv[1] 不是数字)但仍然回答我的问题。
猜你喜欢
  • 1970-01-01
  • 2015-05-28
  • 2017-03-10
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
  • 2015-04-08
相关资源
最近更新 更多