When i pass any argument i get this error: Segmentation fault (core dumped) only -h option works properly.
Here option -d and -a takes string value.
option -s and -e takes integer value.
how can i save the values of options passed to a different variable for later usage ?
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
void usage() {
printf("Usage: help\n----------\n------");
}
int main(int argc, char *argv[]) {
int opt= 0;
int start = -1, end = -1;
char *alg,*dir;
static struct option long_options[] = {
{"start",no_argument,0,'s' },
{"end",no_argument,0,'e' },
{"algorithm",no_argument, 0,'a' },
{"directory",required_argument, 0,'d' },
{0,0,0,0}
};
int long_index =0;
while ((opt = getopt_long(argc, argv,"seadh:",
long_options, &long_index )) != -1) {
switch (opt) {
case 'a' :
printf("you entered \"%s\"\n", optarg); // error while printing this
break;
case 'd' :
printf("you entered \"%s\"\n", optarg);
break;
case 's' : start = atoi(optarg);
break;
case 'e' : end = atoi(optarg);
break;
case 'h' : usage();
break;
default: usage();
exit(EXIT_FAILURE);
}
}
printf("%d",start);
return 0;
}
I want to use the options passed along with their values to call a python script inside this C program using system Call .
eg : system(python alg.py -alr -s3 -e45 -d/path)