简体   繁体   English

c 中终端中执行行的输入

[英]Input from the execution line in the terminal in c

The problem that i have is that i have to write a hanois tower game in c and the input for the number of the rings must not be in the programm but the code must read the number of rings in the execution.我遇到的问题是我必须用 c 编写一个 hanois 塔游戏,并且环数的输入不能在程序中,但代码必须在执行中读取环数。

Example: ./ hanoistower 3示例: ./ hanoistower 3

And the code should get the 3 as the input.并且代码应该将 3 作为输入。 How can i do that?我怎样才能做到这一点?

Command line arguments are propagated as strings through the main() function of your C program.命令行参数通过 C 程序的 main() 函数作为字符串传播。

In int main(int argc, char *argv[]) argc is the number of arguments, and argv is an array of strings containing the arguments.int main(int argc, char *argv[]) argc 是参数的数量,argv 是包含参数的字符串数组。 Note that the program name itself is always the first "argument".请注意,程序名称本身始终是第一个“参数”。

As the arguments are passed as strings, you likely need to convert your 3 to an integer, which can be done with the atoi function.由于参数作为字符串传递,您可能需要将3转换为整数,这可以使用atoi函数完成。 Here's a start:这是一个开始:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   int rings;
   if(argc != 2) {
       printf("Usage: %s number-of-rings\n",argv[0]);
       return 1;
   }

   rings = atoi(argv[1]);
   printf("Using number-of-rings = %d\n", rings);
...

   return 0;
}

I strongly suggest reading a good C programming book (in 2020, Modern C ).我强烈建议阅读一本好的 C 编程书(2020 年,现代 C )。

It will be much faster than asking questions here.这比在这里提问要快得多。 Don't forget to also read the documentation of your C compiler (perhaps GCC ), your build automation tool (eg GNU make or ninja ), and debugger (perhaps GDB ).不要忘记阅读 C 编译器(可能是GCC )、构建自动化工具(例如GNU makeninja )和调试器(可能是GDB )的文档。 If you code on and for Linux, read also Advanced Linux Programming and syscalls(2) , and the documentation of GNU glibc (or of musl-libc , if you use it).如果您在 Linux 上编写代码,请同时阅读Advanced Linux Programming and syscalls(2)以及GNU glibc (或musl-libc ,如果您使用它)的文档。

Hovever, the program arguments is given as a null terminated array of strings to the main function, which is usually declared as然而,程序参数作为一个空终止的字符串数组提供给主函数,通常声明为

 int main (int argc, char**argv) { /*...*/ }

if you run your program with ./hanoistower 3 and if your hanoistower.c is your source code (which you need to compile with debugging and warning enabled, ie gcc -Wall -g hanoistower.c -o hanoistower on Linux) then you have one extra argument, so如果您使用./hanoistower 3运行您的程序,并且您的hanoistower.c是您的源代码(您需要在启用调试和警告的情况下进行编译,即 Linux 上的gcc -Wall -g hanoistower.c -o hanoistower )一个额外的参数,所以

  1. argc == 2
  2. argv[0] is the "./hanoistower" string argv[0]"./hanoistower"字符串
  3. argv[1] is the "2" string (use atoi to convert it to an int ) argv[1]"2"字符串(使用atoi将其转换为int
  4. argv[2] is NULL argv[2]NULL

Please, please learn to use the debugger ( gdb on Linux).请学习使用调试器(Linux 上的gdb )。

Just add, argc and argv to the list of main method parameters, as shown below:只需将argcargv添加到main方法参数列表中,如下所示:

int main ( int argc, char *argv[] )

Then use argv as the variable to specify number of rings inside your code.然后使用argv作为变量来指定代码中的环数。

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

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