Linux 如何将参数从终端传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12585398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:26:37  来源:igfitidea点击:

How to pass arguments from terminal to a function

clinuxterminal

提问by user1072706

Possible Duplicate:
Pass arguments into C program from command line

可能的重复:
从命令行将参数传递给 C 程序

I am trying to pass three arguments from terminal into a function called replace. I would like to know if it is possible to do the following from terminal

我试图将三个参数从终端传递到一个名为 replace 的函数中。我想知道是否可以从终端执行以下操作

  % ./replace d DDD mytest.tx

I have looked online but can only find information on passing values directly to main() and not the function inside.

我在网上查看过,但只能找到有关将值直接传递给 main() 而不是内部函数的信息。

Edit: I have edited the main functions as following:

编辑:我编辑了主要功能如下:

void replace(char* string_a, char* string_b, char* string_f)
 {
  }

int main(int argc, char *argv[])
  { 
       if(argc < 4)
  { 
    printf("Not enough arguments\n");
   return 0;
  }

replace(argv[1],argv[2],argv[3]);
 }

采纳答案by Lews Therin

Your main function should be in the format:

您的主要功能应采用以下格式:

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

argv is a pointer to your arguments. Note that the first argument is the name of your program.

argv 是指向您的参数的指针。请注意,第一个参数是程序的名称。

Here is a lessonon command line arguments:

这是有关命令行参数的 课程

回答by unwind

All stand-alone proper C programs start their execution in main(). That's just how the language works.

所有独立的适当 C 程序都以main(). 这就是语言的工作方式。

So what you need to do is call replace()from main(), after verifying and interpreting the arguments. The command-line arguments will be in the standard int argc, char *argv[]parameters to main().

所以你需要做的是在验证和解释参数之后调用replace()from main()。命令行参数将在标准int argc, char *argv[]参数中main()

回答by Tomislav Dyulgerov

The thing you want to do is use main's argument list.

您想要做的是使用main的参数列表。

You can use the following signature:

您可以使用以下签名:

int main(int argc, char* argv[]), where argv is a pointer to the argument list, passed from the command line.

int main(int argc, char* argv[]),其中 argv 是从命令行传递的指向参数列表的指针。

回答by Simon MILHAU

Course you can, main prototype is int main(int ac, char **av)where ac is the number of arguments passed to the program and char** is an array of arrays containing the arguments passed to the program.

当然可以,主要原型是int main(int ac, char **av)其中 ac 是传递给程序的参数数量,而 char** 是包含传递给程序的参数的数组数组。

For example for your code :

例如对于您的代码:

int main(int ac, char **av)
     {
      void replace (av[1], av[2], av[3])
        { 
         .......
        }

     }

If you launch your exec as : ./replace d DDD mytest.tx, av[0] will be your program name, av[1] will be d, av[2] DDDand av[3] mytest.tx

如果您将 exec 启动为 : ./replace d DDD mytest.tx, av[0] 将是您的程序名称, av[1] 将是d, av[2]DDD和 av[3]mytest.tx

Good luck !

祝你好运 !

回答by 123

Look up argcand argv.

抬头看argcargv

argc is the number of arguments and argv is an array of pointers to your arguments.

argc 是参数的数量, argv 是指向参数的指针数组。

so your code should look something like this:

所以你的代码应该是这样的:

void replace (char string_a[],char string_b[], char string_f[])
{
    //...
}

int main(int argc, char *argv[])
{
    if(argc < 4)
    {
        printf("Not enough arguments\n");
        return 0;
    }

    replace(argv[1], argv[2], argv[3]);
}

Also remember that the first item in argv (argv[0]) is the path of the executing program.

还要记住,argv( argv[0])中的第一项是执行程序的路径。

回答by Mike

To answer your exact question: I am trying to pass three arguments from terminal into a function called replace.No, you cannot by-pass sending the arguments to main.

要回答您的确切问题: I am trying to pass three arguments from terminal into a function called replace.不,您不能绕过将参数发送到 main。

The C language is defined such that it looks for a main()entry point into the program, it complains if you don't have one. Because main()is expected to be present it is used as the entry point for any command line arguments:

C 语言的定义是,它会寻找程序的main()入口点,如果没有入口点,它就会抱怨。因为main()预计会出现它被用作任何命令行参数的入口点:

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

where argcis the number of command line arguments and argv[]is an array of character pointers ("string" versions of your argument list)

其中argc是命令行参数的数量,argv[]是字符指针数组(参数列表的“字符串”版本)

If you want to pass them to replace you really just have to send them straight through:

如果你想通过它们来替换你真的只需要直接发送它们:

int main(int argc, char *argv[]){
  if argc is big enough to satisfy
     replace(argv's)

If you really don't want main()knowing about the arguments you need to store they in a file or get them at run time from stdinfrom your replace()function.

如果您真的不想main()知道需要将它们存储在文件中或在运行时stdin从您的replace()函数中获取它们的参数。

If you're trying to have "replace" BE your main function, that can be possible if your linker allows you to define the entry point.

如果您尝试将“替换”成为您的主要功能,那么如果您的链接器允许您定义入口点,那是可能的。

Again... not really sure what your goal is here.

再次......不太确定你的目标是什么。