简体   繁体   English

在C中从命令行输入多个输入参数

[英]multiple input parameters from command line in C

I am trying to pass multiple parameters when running a compiled C code 我试图在运行已编译的C代码时传递多个参数

code would be like this 代码会像这样

void main(char argc,char *argv[]){

    printf("%s",argv[1])    //filename
    FILE *file = fopen(argv[1], "r")

    printf("%s",argv[2])    //function to be called
    char* func_name = argv[2];

    printf("%s",argv[3])    //how many times the function is called
    int repeat = argv[3];

    for(int i=0;i<repeat;i++){
        func_name(file) //calls some function and passes the file to it 
    }

} }

i would compile like this 我会这样编译

gcc cprog.c -o cprog

run like - 像-

./cprog textfile.txt function1 4 

how do i do this ? 我该怎么做呢 ? any help would be appreciated ! 任何帮助,将不胜感激 !

To be able to call a function that you have as a string, you have know which name is paired to which function. 为了能够以字符串形式调用函数,您已经知道哪个名称与哪个函数配对。

If all functions take the same arguments, you can have an array of structures with name and function pointer, and then match the name with the correct entry in the table. 如果所有函数都采用相同的参数,则可以具有名称和函数指针的结构数组,然后将名称与表中的正确条目匹配。

Otherwise, if the arguments are different you have to have a chain of strcmp calls to call the correct function. 否则,如果参数不同,则必须有一系列strcmp调用才能调用正确的函数。

There are a lots of error here. 这里有很多错误。

int repeat = argv[3]; //You must convert char* to int before assignment.
func_name(file)       //func_name is a char* not a function. C does not support reflection so there is no way to call function like this.

First off: 首先:

  1. You are missing some semicolons, so your code won't even compile. 您缺少一些分号,因此您的代码甚至无法编译。
  2. argv[] are strings , so you'll have to convert them to integers if you want to use them as such. argv[]字符串 ,因此,如果要按原样使用它们,则必须将它们转换为整数。
  3. C does not store function names in the binary, so you have to create some kind of calling table. C不在二进制文件中存储函数名称,因此您必须创建某种调用表。

Below find a working example. 下面找到一个工作示例。 I creates a struct that maps a name to a function, implement that function and go look for it. 我创建了一个struct ,一个名称映射到一个功能,实现该功能,去寻找它。 It's quite buggy (no input validation is done), but gives you a proof of concept on how to possibly implement this. 它有很多错误(没有完成输入验证),但是为您提供了有关如何实现此概念的概念证明。

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

struct fcn_entry {
  char *name;
  void (*fcn)(char *);
};

void fcn1(char *fn) {
   printf("fcn1: %s\n", fn);
}

void fcn2(char *fn) {
   printf("fcn2: %s\n", fn);
}

void main(char argc,char *argv[]){
    // name-to-function table
    struct fcn_entry entries[] = {
        { "fcn1", fcn1 },
        { "fcn2", fcn2 },
        { NULL, NULL }
    };
    void (*fcn_to_call)(char *);
    int i = 0;

    printf("%s",argv[1]);    //filename

    printf("%s",argv[2]);    //function to be called    
    char* func_name = argv[2];
    i = 0;
    while(entries[i].name != NULL) {
        if (strcmp(entries[i].name, func_name) == 0) {
           fcn_to_call = entries[i].fcn;
           break;
        } else {
           fcn_to_call = NULL;
        }
        i++;
    }


    printf("%s",argv[3]);    //how many times the function is called
    int repeat = atoi(argv[3]);

    for(i=0;i<repeat;i++){
        fcn_to_call(argv[1]);
    }
}

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

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