i want to make C program, “animal.c”.
and in shell,
when i type
>animal -cat
the result is “meow”
>animal -dog
the result is “bow”.
is it only way?
void main(int argc, char **argv){
if(argv[1][0] == '-' && argv[1][1] == 'c' && argv[1][2] == 'a' && argv[1][3] == 't')
printf("meow");
if(argv[1][0] == '-' && argv[1][1] == 'd' && argv[1][2] == 'o' && argv[1][3] == 'g')
printf("bow");
}
in other case, when i want to make file ‘cat’ which locates in “/animal” ,
assuming that the folder ‘animal’ already locates in root directory.
animal -cat
the result is making file “cat” in “/animal”
the file “cat”‘s absolute path is “/animal/cat”
how to do ?
is there another way to receive option value?
getopt is a popular and an almost standard way of parsing command line options. You should use that (or something similar) rather than parsing command line arguments by hand. It’s tremendously error prone.