Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 有没有更好的方法来检查pid输入是否都是数字?_C - Fatal编程技术网

C 有没有更好的方法来检查pid输入是否都是数字?

C 有没有更好的方法来检查pid输入是否都是数字?,c,C,所以我有一个程序,它接受PID输入和一个字符 $ ./transmit 1111 a 我的问题是。如果是 $ ./transmit 111a x 因为PID都是数字,所以我需要抓住它 鉴于: char *a[] = {"./transmit", "111a", "x"}; 如何检查“111a”是否仅为数字?isdigit仅在角色中起作用。我必须循环整个输入吗 您可以使用以下功能: #include <stdio.h> #include <string.h> int

所以我有一个程序,它接受PID输入和一个字符

$ ./transmit 1111 a
我的问题是。如果是

$ ./transmit 111a x
因为PID都是数字,所以我需要抓住它

鉴于:

char *a[] = {"./transmit", "111a", "x"};
如何检查“111a”是否仅为数字?isdigit仅在角色中起作用。我必须循环整个输入吗

您可以使用以下功能:

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]) {
    if (argc > 1) {
        if (argv[1][strspn(argv[1], "0123456789")] == '\0') {
            puts("Yes, the first command line argument is all numeric.");
        }
        else {
            puts("No, the first command line argument is not all numeric.");
        }
    }
    else {
        puts("Please provide an argument on the command line");
    }
    return 0;
}
#包括
#包括
int main(int argc,char*argv[]){
如果(argc>1){
如果(argv[1][strspn(argv[1],“0123456789”)]=='\0'){
puts(“是的,第一个命令行参数都是数字。”);
}
否则{
puts(“不,第一个命令行参数不全是数字。”);
}
}
否则{
puts(“请在命令行中提供参数”);
}
返回0;
}
您可以使用以下功能:

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]) {
    if (argc > 1) {
        if (argv[1][strspn(argv[1], "0123456789")] == '\0') {
            puts("Yes, the first command line argument is all numeric.");
        }
        else {
            puts("No, the first command line argument is not all numeric.");
        }
    }
    else {
        puts("Please provide an argument on the command line");
    }
    return 0;
}
#包括
#包括
int main(int argc,char*argv[]){
如果(argc>1){
如果(argv[1][strspn(argv[1],“0123456789”)]=='\0'){
puts(“是的,第一个命令行参数都是数字。”);
}
否则{
puts(“不,第一个命令行参数不全是数字。”);
}
}
否则{
puts(“请在命令行中提供参数”);
}
返回0;
}
char*err;
无符号长pid=strtoul(argv[1],&err,10);
如果(*err | | err==argv[1])
错误();
如果((pid|t)pid!=pid | |(pid|t)pid
char*err;
无符号长pid=strtoul(argv[1],&err,10);
如果(*err | | err==argv[1])
错误();

如果((pid_t)pid!=pid| |(pid_t)pid,您可能对
strtol
感兴趣。您可能对
strtol
感兴趣。次要:不清楚为什么要使用
strtoul()
pid_t
不是有符号整数类型吗
pid_t
的负值(和
0
)不代表真正的pid,但仅用于特殊目的(错误信号,组ID在
waitpid()
中。此代码没有正确检查负值。
(pid\u t)pid!=pid
没有任何作用。通过使用
strtol()
,可以检测到负值,这与
strtoul()不同)
。超出有符号范围
pid\t
的值也有问题。将无符号转换为超出范围有符号(
pid\t)pid
)是一种ID行为。最好进行全范围检测,不允许任何值进入不在范围内的
pid\t
。例如,如果
argv[1]=“-1”
pid\u t
无符号长/long
宽度相同:
strtoul()
返回
ULONG\u MAX
(pid\u t)pid!=pid通常为假-检测不到负值。通过转换为
strtol()
,可以检测到负输入字符串,并且不会像此答案那样传递负值或值>
LONG\u MAX
。次要:不清楚为什么使用
strtoul()
?是否
pid\u t
是有符号整数类型?负值(和
0
)对于
pid\u t
不表示真正的pid,但仅用于特殊目的(错误信号,组ID在
waitpid()
中)。此代码不会正确检查负值。
(pid\u t)pid!=pid
没有任何用途。通过使用
strtol()
,可以检测到负值,而不像
strtoul()
。超出有符号范围
pid\t
的值也有问题。将无符号转换为超出范围有符号(
pid\t)pid
)是一种ID行为。最好进行全范围检测,不允许任何值进入不在范围内的
pid\t
。例如,如果
argv[1]=“-1”
pid\u t
无符号长/long
宽度相同:
strtoul()
返回
ULONG\u MAX
(pid\u t)pid!=pid通常为假-检测不到负值。通过转换为
strtol()
,可以检测到负输入字符串,并且不会像此答案那样传递负值或值>
LONG\u MAX