简体   繁体   English

如何检查在 C 中的命令行中传递的参数?

[英]How to check arguments passed in command line in C?

I am writing a program in C for a basic calculator.我正在用 C 语言编写一个用于基本计算器的程序。 I am trying to do this using what I have learned so far: printf() and scanf() functions.我正在尝试使用我迄今为止学到的知识来做到这一点:printf() 和 scanf() 函数。 I am passing arguments into my program through the command line.我正在通过命令行将参数传递到我的程序中。 I am assuming three arguments will be passed at a time which includes: first int, an operator, and the second int.我假设将一次传递三个参数,其中包括:第一个 int、一个运算符和第二个 int。 I want to check if the second arg passed is an operator and then check if it's +,-,*... so on.我想检查传递的第二个 arg 是否是一个运算符,然后检查它是否是 +,-,*... 等等。 Here is what I came up with:这是我想出的:

int main(int argc, char **argv) {
        scanf("%d %c %d", &a, &oper, &b);
        if (oper != 43) {
                  printf("Error: Operator is not a +");
                  return(1);
        }
}

So obviously, I have omitted a lot of the code and kept the relevant part.所以很明显,我省略了很多代码并保留了相关部分。 Here I am just checking if the oper is a +.在这里,我只是检查操作符是否为 +。 The ASCII key is 43 so I thought this would work but no luck! ASCII 密钥是 43,所以我认为这会起作用,但没有运气! Any ideas?有任何想法吗? (I would like to see if I can do this just with printf and scanf if possible) (如果可能的话,我想看看我是否可以只用 printf 和 scanf 来做到这一点)

EDIT: For example if 12 b 13 was entered, it should return the error above.编辑:例如,如果输入了 12 b 13,它应该返回上面的错误。 Same goes for '10 +a 10' or '10 ++ 10'. '10 +a 10' 或 '10 ++ 10' 也是如此。

Firstly I would highly recommend looking at the man-pages for any C library function you come across, they have a lot of useful information.首先,我强烈建议您查看您遇到的任何 C 库函数的手册页,它们有很多有用的信息。 It seems like you are using scanf() improperly as it is not made to be used with command line arguments.似乎您使用 scanf() 不正确,因为它没有与命令行参数一起使用。

You can check for matches for a single character by comparing the argument like this:您可以通过像这样比较参数来检查单个字符的匹配项:

if(argv[2][0] == '+') ...

(argv[0] is the program's file name). (argv[0] 是程序的文件名)。

If would would like to compare string you can use strcmp().如果想比较字符串,可以使用 strcmp()。 But for the operator example you can get away with just checking the first and second characters in the argument like this:但是对于运算符示例,您可以像这样检查参数中的第一个和第二个字符:

if(argv[2][0] == '+' && argv[2][0] == '\0') ...

What this does is compare the first two characters of the argument.这样做是比较参数的前两个字符。 It first checks for the '+' and then checks if that is the end of the string with by checking for the null terminator '\\0' .它首先检查'+' ,然后通过检查空终止符'\\0'检查它是否是字符串的结尾。

We can make the assumption that any argument has at least two characters, the visible character and a null terminator.我们可以假设任何参数至少有两个字符,可见字符和空终止符。 Performing this on other strings has no guarantee of this however.但是,在其他字符串上执行此操作并不能保证这一点。

The other characters, specifically the numbers need to be converted from their respective ASCII values to integers.其他字符,特别是数字需要从它们各自的 ASCII 值转换为整数。 You can use atoi or strtol to do this, although atoi will most likely be easier for you.您可以使用 atoi 或 strtol 来执行此操作,尽管 atoi 对您来说很可能更容易。

As David C. Rankin pointed out, **argv is a double pointer which at a high level and in most cases you can treat as a double array.正如 David C. Rankin 指出的那样, **argv是一个双指针,在高层次上,在大多数情况下,您可以将其视为双数组。 In C a string is actually just an array of type char, so what argv[2] is doing above is first accessing the third index of **argv , this is now de-referenced to a type char * where the string (char array) is located.在 C 中,字符串实际上只是一个 char 类型的数组,因此上面的argv[2]所做的是首先访问**argv的第三个索引,现在将其取消引用到类型char *其中字符串 (char array ) 位于。 This can then further be de-referenced by the [0] in argv[2][0] to look at the first char of the string.然后可以通过argv[2][0][0]进一步取消引用以查看字符串的第一个字符。

Code example:代码示例:

char **my_arrays = argv; // a array of arrays
char *array = *argv;       // de-references to index 0 in argv
char *array = *(argv + 1); // de-references to index 1 in argv
char *array = argv[0];     // de-references to index 0 in argv
char *array = argv[1];     // de-references to index 1 in argv

char first_char = *(*argv)   // the first char of the first array of argv
char first_char = *(argv[0]) // the same as above
char first_char = argv[0][0] // the same as above

A side note.旁注。 All strings in C should end in a null terminator which can be represented by NULL , 0 , or '\\0' values. C 中的所有字符串都应以空终止符结尾,该终止符可以由NULL0'\\0'值表示。 This will represent the end of the string and many C functions rely on this to know when to stop.这将代表字符串的结尾,许多 C 函数依靠它来知道何时停止。 Also NULL is technically a C macro, but you don't need to treat it any differently than 0 because it literally just expands to 0 .此外NULL从技术上讲是一个 C 宏,但您不需要将其与0区别对待,因为它实际上只是扩展为0

  1. It's char **argv .它是char **argv As Some programmer dude said, you should reread your book/tutorial.正如一些程序员说的那样,你应该重读你的书/教程。
  2. scanf doesn't read arguments. scanf不读取参数。 It reads from stdin.它从标准输入读取。
  3. Arguments are of type char* and are stored in argv.参数的类型为char*并存储在 argv 中。 To convert these arguments to integers, use atoi or strtol (preferably strtol).要将这些参数转换为整数,请使用atoistrtol (最好是 strtol)。 See this for more info.有关更多信息,请参阅内容。
  4. If you want to read from stdin using scanf, that is fine, and what you have will work as long as you instead input the data into stdin, and not as command line arguments.如果您想使用 scanf 从标准输入读取,那很好,只要您将数据输入标准输入,而不是作为命令行参数,您所拥有的内容就可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM