C-读取命令行参数 [英] C - reading command line parameters

查看:59
本文介绍了C-读取命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了很少的程序来计算pi(π)作为整数.现在,我面临一个问题,如何扩展它以计算积分,在启动应用程序时将作为附加参数给出该积分.我该如何在程序中处理这样的参数?

解决方案

编写主函数时,通常会看到以下两个定义之一:

  • int main(void)
  • int main(int argc,char ** argv)

第二种形式将允许您访问传递给程序的命令行参数以及指定的参数数量(参数以空格分隔).

main 的参数为:

  • int argc -运行程序时传递给您的程序的参数数量.至少为 1 .
  • char ** argv -这是指向 char * 的指针.也可以是: char * argv [] ,表示" char * 的数组".这是C样式字符串指针的数组.

基本示例

例如,您可以执行以下操作以打印出传递给C程序的参数:

  #include< stdio.h>int main(int argc,char ** argv){对于(int i = 0; i< argc; ++ i){printf("argv [%d]:%s \ n",i,argv [i]);}} 

我正在使用GCC 4.5编译一个名为 args.c 的文件.它将编译并构建默认的 a.out 可执行文件.

  [birryree @ lilun c_code] $ gcc -std = c99 args.c 

现在运行它...

  [birryree @ lilun c_code] $ ./a.out你好argv [0]:./a.outargv [1]:您好argv [2]:在那里 

因此您可以看到,在 argv 中, argv [0] 是您运行的程序的名称(这不是标准定义的行为,但是很常见.您的论点从 argv [1] 及其以后的地方开始.

所以基本上,如果您想要一个参数,可以说...

./myprogram积分


适合您的简单案例

您可以检查 argv [1] 是否为整数,例如 strcmp("integral",argv [1])== 0 .

所以在您的代码中...

  #include< stdio.h>#include< string.h>int main(int argc,char ** argv){if(argc< 2)//没有传递任何参数{//做一点事}if(strcmp(积分",argv [1])== 0){runIntegral(...);//或者其他的东西}别的{//做其他事情.}} 


更好的命令行解析

当然,这都是非常基本的,并且随着程序变得越来越复杂,您可能需要更高级的命令行处理.为此,您可以使用 GNU getopt 之类的库..>

I have made little program for computing pi (π) as an integral. Now I am facing a question how to extend it to compute an integral, which will be given as an extra parameter when starting an application. How do I deal with such a parameter in a program?

解决方案

When you write your main function, you typically see one of two definitions:

  • int main(void)
  • int main(int argc, char **argv)

The second form will allow you to access the command line arguments passed to the program, and the number of arguments specified (arguments are separated by spaces).

The arguments to main are:

  • int argc - the number of arguments passed into your program when it was run. It is at least 1.
  • char **argv - this is a pointer-to-char *. It can alternatively be this: char *argv[], which means 'array of char *'. This is an array of C-style-string pointers.

Basic Example

For example, you could do this to print out the arguments passed to your C program:

#include <stdio.h>

int main(int argc, char **argv)
{
    for (int i = 0; i < argc; ++i)
    {
        printf("argv[%d]: %s\n", i, argv[i]);
    }
}

I'm using GCC 4.5 to compile a file I called args.c. It'll compile and build a default a.out executable.

[birryree@lilun c_code]$ gcc -std=c99 args.c

Now run it...

[birryree@lilun c_code]$ ./a.out hello there
argv[0]: ./a.out
argv[1]: hello
argv[2]: there

So you can see that in argv, argv[0] is the name of the program you ran (this is not standards-defined behavior, but is common. Your arguments start at argv[1] and beyond.

So basically, if you wanted a single parameter, you could say...

./myprogram integral


A Simple Case for You

And you could check if argv[1] was integral, maybe like strcmp("integral", argv[1]) == 0.

So in your code...

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    if (argc < 2) // no arguments were passed
    {
        // do something
    }

    if (strcmp("integral", argv[1]) == 0)
    {
        runIntegral(...); //or something
    }
    else
    {
        // do something else.
    }
}


Better command line parsing

Of course, this was all very rudimentary, and as your program gets more complex, you'll likely want more advanced command line handling. For that, you could use a library like GNU getopt.

这篇关于C-读取命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆