I am not sure where i am missing. I want to catch some characters from command line. I am using getopt but not sure how to copy from optarg. Please help me i am not very sure about character/string handling in c.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
main(int argc , char *argv[]) {
char *file;
int opt;
while ( ( opt = getopt(argc, argv, "f:") ) != -1 ){
switch(opt){
case 'f':
file=(char *) malloc(2);
strcpy(file,optarg);
printf("\nValue of file is %c\n",file);
break;
default :
return(1);
}
}
return(0);
}