如何解析整数命令行参数使用C? [英] How to parse integer command line arguments in C?
问题描述
我想一个用户通过其中两个参数或留空。例如:
I would like a user to pass either two parameters or leave it blank. For instance:
./program 50 50
或
./program
当我试图用 INT主(INT ARGC,CHAR *的argv [])
,第一件事情我已经做了修改的char *的argv []
到为int *的argv []
,但没有奏效。我想是从用户只是为0和100之间的输入的两个整数因此,如果它不是两个整数那么它应该给出一个错误。
When I tried to use int main(int argc, char *argv[])
, first thing I have done was to change char *argv[]
to int *argv[]
but it did not work. What I want is from the user is just to enter two integers between 0 and 100. So if it is not two integers then it should give an error.
我是那种想放弃了与类型错误(为我所用的C#程序),但不管我进入,ARGV [1]是'符'类型的所有时间。
I was sort of thinking to give out an error with types (as I used to program on C#) but whatever I enter, argv[1] would be 'char' type all the time.
所以我所做的就是
for (int i = 0; i <= 100; i++)
{
//printf("%d", i);
if (argv[1] == i)
{
argcheck++;
printf("1st one %d\n", i);
}
else if (argv[2] == i)
{
argcheck++;
printf("2nd one %d\n", i);
}
这不能正常工作。此外,它提供了编译时警告,但如果我改变的argv
与的atoi(ARGV [1])
的实例,那么它给了分段故障(核心转储)错误。
This does not work as well. Also it gives warning when compiling, but if I change argv
with atoi(argv[1])
for instance, then it gives a Segmentation fault (core dumped) error.
我需要一种简单的方式来解决这个问题。
I need a simple way to solve this problem.
编辑:
所以我固定的atoi()
,为什么它被赋予段故障的原因是因为我是用null值尝试它,当我没有参数。所以我固定它通过增加一个额外COND。但现在的问题是,如果该值假设
So I fixed with atoi()
, the reason why it was giving segmentation fault was because I was trying it with null value when I have no parameter. So I fixed it up by adding an extra cond. But now the problem is if the value is let's say
./program asd asd
然后的atoi的输出(ARGV [1])
将是0。那么,有没有办法改变这个值?
Then the output of atoi(argv[1])
would be 0. So is there a way to change this value?
推荐答案
不要使用的atoi()和不使用与strtol()。 ATOI()没有错误检查(如你发现了!),并与strtol()必须错误检查使用全局errno变量,这意味着你必须给errno设置为0,然后调用与strtol(),然后再检查errno错误。更好的方法是使用的sscanf(),它也可以让你从一个字符串,而不仅仅是一个整数分析任何原始类型,它可以让你读花式格式(如十六进制)。
Don't use atoi() and don't use strtol(). Atoi() has no error checking (as you found out!) and strtol() has to be error-checked using the global errno variable, which means you have to set errno to 0, then call strtol(), then check errno again for errors. A better way is to use sscanf(), which also lets you parse any primitive type from a string, not just an integer, and it lets you read fancy formats (like hex).
例如,要分析从字符串整数1435:
For example, to parse integer "1435" from a string:
if (sscanf (argv[1], "%i", &intvar)!=1) { printf ("error - not an integer"); }
从字符串解析单个字符Z
To parse a single character 'Z' from a string
if (sscanf (argv[1], "%c", &charvar)!=1) { printf ("error - not a char"); }
从字符串解析漂3.1459
To parse a float "3.1459" from a string
if (sscanf (argv[1], "%f", &floatvar)!=1) { printf ("error - not a float"); }
要从一个字符串分析一个大的无符号十六进制整数0x332561
To parse a large unsigned hexadecimal integer "0x332561" from a string
if (sscanf (argv[1], "%xu", &uintvar)!=1) { printf ("error - not a hex integer"); }
如果您需要更多的错误处理比,使用正则表达式库。
If you need more error-handling than that, use a regex library.
这篇关于如何解析整数命令行参数使用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!