Sid5427
Sid5427

Reputation: 743

multiple input parameters from command line in C

I am trying to pass multiple parameters when running a compiled C code

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 !

Upvotes: 0

Views: 9031

Answers (3)

Bart Friederichs
Bart Friederichs

Reputation: 33511

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.
  3. C does not store function names in the binary, so you have to create some kind of calling table.

Below find a working example. I creates a struct that maps a name to a function, implement that function and go look for it. 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]);
    }
}

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409176

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.

Upvotes: 1

Polymorphism
Polymorphism

Reputation: 249

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.

Upvotes: 1

Related Questions