简体   繁体   English

如何获取正确数量的C ++命令行参数?

[英]How to get the correct number of C++ command line arguments?

I start my program with 我从开始我的程序

int main (int argc, char *argv[]) {
    printf("%d \n", argc);

then I compiled in Ubuntu using g++, and I ran the program using 然后我使用g ++在Ubuntu中编译,然后使用

./calc 2 3 4 + *

but the program outputs 17! 但是程序输出17! I did a printf on the arguments as well, they are: 我也对参数进行了printf,它们是:

arg 0: ./calc 
arg 1: 2 
arg 2: 3 
arg 3: 4 
arg 4: + 
arg 5: 1.2.c 
arg 6: 1.3.c 
arg 7: 1.4.c 
arg 8: 2.1.c 
arg 9: 2.2.c 
arg 10: 2.3.c 
arg 11: 2.4.c 
arg 12: 3.2.c 
arg 13: 3.4.c 
arg 14: 4.1.c 
arg 15: a.out 
arg 16: calc 

but obviously that's not what I'm expecting. 但这显然不是我所期望的。 How can I correct this? 我该如何纠正?

The * is being evaluated by your shell to mean all of the files in the current directory. Shell正在评估*表示当前目录中的所有文件。 You should escape the asterisk using \\* . 您应该使用\\*转义星号。

The linux shell interpreted * as a listing of all the files in a directory. linux shell将*解释为目录中所有文件的列表。 Try escaping it with "\\" eg 尝试用“ \\”转义,例如

./calc 2 3 4 + \*

or 要么

./calc 2 3 4 + "*"

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

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