[英]Command line argument validation in C
I have a program that needs to get an int
from the user from the command line我有一个程序需要从命令行从用户那里获取一个
int
int main(int argc, char* argv[])
My only problem is that I need to check whether argv
is an int
.我唯一的问题是我需要检查
argv
是否是int
。 If it isn't, I need to return an error.如果不是,我需要返回一个错误。 How can I do that?
我怎样才能做到这一点? I have to check if the input is an
int
before using atoi
.在使用
atoi
之前,我必须检查输入是否为int
。 Can someone help me?有人能帮我吗?
Here's one way, using strtol
and checking the end of the string:这是一种方法,使用
strtol
并检查字符串的结尾:
#include <stdio.h>
#include <stdlib.h>
int
main(int argc,char **argv)
{
char *cp;
long lval;
int val;
// skip over program name
--argc;
++argv;
if (argc < 1) {
fprintf(stderr,"main: no argument specified\n");
exit(1);
}
cp = *argv;
if (*cp == 0) {
fprintf(stderr,"main: argument an empty string\n");
exit(1);
}
lval = strtol(cp,&cp,10);
if (*cp != 0) {
fprintf(stderr,"main: argument '%s' is not an integer -- '%s'\n",
*argv,cp);
exit(1);
}
val = (int) lval;
// NOTE: just going for extra credit here ;-)
// ensure number fits in a int (since strtol returns long and that's 64
// bits on a 64 bit machine)
#if 1
if (val != lval) {
fprintf(stderr,"main: argument '%s' (with value %ld) is too large to fit into an integer -- truncated to %d\n",
*argv,lval,val);
exit(1);
}
#endif
printf("val=%d\n",val);
return 0;
}
UPDATE:更新:
Minor: Code does not detect conversion overflow of strtol() Code incorrectly assumes range of long more than int.
次要:代码未检测到 strtol() 的转换溢出 代码错误地假定 long 的范围大于 int。 If same range,
if (val != lval)
is always true.如果范围相同,
if (val != lval)
始终为真。 Suggest looking aterrno, INT_MAX,INT_MIN
建议查看
errno, INT_MAX,INT_MIN
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
int
main(int argc,char **argv)
{
char *cp;
long lval;
int val;
// skip over program name
--argc;
++argv;
if (argc < 1) {
fprintf(stderr,"main: no argument specified\n");
exit(1);
}
cp = *argv;
if (*cp == 0) {
fprintf(stderr,"main: argument an empty string\n");
exit(1);
}
errno = 0;
lval = strtol(cp,&cp,10);
if (*cp != 0) {
fprintf(stderr,"main: argument '%s' is not an integer -- '%s'\n",
*argv,cp);
exit(1);
}
// on a 32 bit machine, entering 2147483648 will produce a non-zero errno
if (errno) {
fprintf(stderr,"main: argument '%s' parse error -- '%s'\n",
*argv,strerror(errno));
exit(1);
}
// on a 64 bit machine, entering 2147483648 will not produce an error, so
// we should check the range ourselves
if ((lval < INT_MIN) || (lval > INT_MAX)) {
fprintf(stderr,"main: argument '%s' range error -- %ld outside of range (%ld to %ld)\n",
*argv,lval,(long) INT_MIN,(long) INT_MAX);
exit(1);
}
val = (int) lval;
// NOTE: just going for extra credit here ;-)
// ensure number fits in a int (since strtol returns long and that's 64
// bits on a 64 bit machine)
// FIXME -- with above tests this can never be true (i.e. fault), so
// I've nop'ed it -- left in to show prior/original test
#if 0
if (val != lval) {
fprintf(stderr,"main: argument '%s' (with value %ld) is too large to fit into an integer -- truncated to %d\n",
*argv,lval,val);
exit(1);
}
#endif
printf("val=%d\n",val);
return 0;
}
You can try to convert the argument using strtol()
, it will return 0
if the value is not parseable or the parsed value.您可以尝试使用
strtol()
转换参数,如果该值不可解析或已解析值,它将返回0
。
You can also use the second argument for a more detailed verification of the input, you can differenciate between a bad input or a 0
input, since in both cases the returned value is 0
.您还可以使用第二个参数对输入进行更详细的验证,您可以区分错误输入或
0
输入,因为在这两种情况下,返回值都是0
。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[])
{
long parsed_value = 0;
int value = 0;
//for command + 1 argument
if (argc == 2)
{
errno = 0;
char *end_ptr;
parsed_value = strtol(argv[1], &end_ptr, 10);
//argument check, overflow, trailing characters, underflow, errno
if(*end_ptr == argv[1][0] || *end_ptr != '\0' || errno == ERANGE
|| parsed_value < INT_MIN || parsed_value > INT_MAX){
fprintf(stderr, "Invalid argument");
return EXIT_FAILURE;
}
}
else{
fprintf(stderr, "Wrong number of arguments, %d provided, 2 needed", argc);
return EXIT_FAILURE;
}
//otherwise the value was parsed correctly
value = parsed_value;
printf("%d", value);
}
Command line argument validation in C
C 中的命令行参数验证
I need to check whether argv is an int
我需要检查 argv 是否是 int
1) Test first if argv[]
contains a string by checking argc
. 1) 首先通过检查
argc
来测试argv[]
是否包含字符串。
for (int a = 1; a < argc; a++) {
int_validation(argv[a]);
}
2) Attempt conversion using strtol()
2) 尝试使用
strtol()
进行转换
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
void int_validation(const char *s) {
// If leading space not OK
// isspace() only valid in unsigned char range and EOF.
if (isspace((unsigned char) *s)) {
puts("Fail - leading spaces");
return;
}
// Convert
int base = 0; // Use 10 for base 10 only input
char *endptr;
errno = 0;
long val = strtol(s, &endptr, base);
if (s == endptr) { // When endptr is same as s, no conversion happened.
puts("Fail - no conversion");
return;
}
// detect overflow
if (errno == ERANGE || val < INT_MIN || val > INT_MAX) {
puts("Fail - overflow");
return;
}
// If trailing space OK, seek pass them
while (isspace((unsigned char) *endptr)) {
endptr++;
}
// If trailing non-numeric text bad
if (*endptr) {
puts("Fail - overflow");
return;
}
printf("Success %d\n", (int) val);
return;
}
Adjust return type and messages as desired.根据需要调整返回类型和消息。
Typically input like "1e5"
or "123.0"
, although mathematically a whole number, is not consider valid int
input.通常像
"1e5"
或"123.0"
这样的输入,虽然在数学上是一个整数,但不被认为是有效的int
输入。 Additional code needed to allow those.需要额外的代码来允许这些。
Use isdigit
to test the characters of the argument for "digit-ness", then convert it (or not) based on the results.使用
isdigit
测试“digit-ness”参数的字符,然后根据结果转换(或不转换)。 For example:例如:
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>
bool is_all_digits(char *s)
{
bool b = true;
for( ; *s ; ++s)
if(!isdigit(*s))
{
b = false;
break;
}
return b;
}
int main(int argc, char *argv[])
{
for(int i = 0 ; i < argc ; ++i)
{
if(is_all_digits(argv[i]))
printf("argv[%d] is an integer = %d\n", i, atoi(argv[i]));
else
printf("argv[%d] is not an integer \"%s\"\n", i, argv[i]);
}
return 0;
}
When run with the command-line arguments使用命令行 arguments 运行时
123 "Not a number" 456.789 "Not another number" 10
the following output is produced:生产以下 output:
argv[0] is not an integer "./a.out"
argv[1] is an integer = 123
argv[2] is not an integer "Not a number"
argv[3] is not an integer "456.789"
argv[4] is not an integer "Not another number"
argv[5] is an integer = 10
As others have noted, is_all_digits
doesn't guarantee that a string which represents an integer is parse-able using atoi
or any other routine, but feel free to doctor this to your heart's content.正如其他人所指出的那样,
is_all_digits
不保证代表 integer 的字符串可以使用atoi
或任何其他例程进行解析,但请随意根据您的内心进行修正。 :-) :-)
As an alternative to strtol()
(that is the canonical answer ) you can perform an hand made validation by using isdigit()
function , and also checking for leading sign characters ( +
and -
):作为
strtol()
的替代方法(即规范答案),您可以使用isdigit()
function执行手工验证,并检查前导符号字符( +
和-
):
#include <ctype.h>
#include <bool.h>
bool isValidInteger( char * str )
{
bool ret = true;
if( str )
{
char p = str;
for( int i=0; str[i] != 0; i++ )
{
if ( !isdigit( str[i] ) )
{
if( i == 0 && ( str[i] == '+' || *p == '-' ) && str[i+1] )
continue;
ret = false;
break;
}
}
}
else
{
return false;
}
return ret;
}
This implementation relies on the fact that input string is null terminated.此实现依赖于输入字符串是 null 终止的事实。 But since every
argv[N]
is a null terminated string, we are fine.但由于每个
argv[N]
都是 null 终止的字符串,我们很好。
Usage:用法:
if ( isValidInteger( argv[1] ) )
{
int par = atoi( argv[1] );
}
Note (1): this validator doesn't check against input values that exceed the int
range (from INT_MIN
to INT_MAX
).注意 (1):此验证器不会检查超出
int
范围(从INT_MIN
到INT_MAX
)的输入值。 This is a limitation that might be considered acceptable in many cases.这是一个在许多情况下可能被认为可以接受的限制。
Note (2): this function doesn't trim leading spaces like strto*
do.注意(2):此 function 不会像
strto*
那样修剪前导空格。 If such feature is required, a check like this one can be added at the top of the for-loop
:如果需要这样的功能,可以在
for-loop
的顶部添加这样的检查:
bool flag = true;
if( str[i] == ' ' )
continue;
flag = false;
In this way spaces will be tolerated, but only until flag
is set to false
that is when the first non-space character is encountered.以这种方式,将允许空格,但仅在
flag
设置为false
之前,即遇到第一个非空格字符时。
Try this.尝试这个。 I'll let you sort error messaging however you like:
我会让你按照你喜欢的方式对错误消息进行排序:
#define TRUE 1
#define FALSE 0
int is_integer( char *s )
{
int i = 0 ;
int is_digit;
int is_sign;
while ( s[i] != '\0' )
{
// this test makes the assumption that the code points for
// decimal digits are contiguous. True for ASCII/UNICODE and EBCDIC.
// If you're using some other bizarro encoding, you're out of luck.
is_digit = s[i] >= '0' && s[i] <= '9' ? TRUE : FALSE ;
is_sign = i == 0 && s[i] == '-' ? TRUE : FALSE ;
if ( !is_digit && !is_sign )
{
return FALSE;
}
++i;
}
return TRUE;
}
int main( int argc, char *argv[] )
{
int i = 0 ;
int cc = 0 ; // assume success;
for ( i = 0 ; i < argc ; ++i )
{
if ( !is_integer(argv[i]) )
{
cc = 1;
}
}
return cc; // exit code 0 is success; non-zero exit code is failure.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.