Here, i have a program, which takes arguments (how surprising...). I want him to have several arguments, as:
./myprogram -f filename.txt -x -o
so i want main args with "-", and these arg shall accept an other arg, in the example, a filename.
I have this structure, very simple:
int main(int argc, char *argv[])
{
printf("Program name: %s\n", argv[0]);
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'f':
printf("%s\n",&argv[1][3]);
break;
case 'd':
printf("%s\n",&argv[1][2]);
printf("%s\n",&argv[1][2]);
break;
default:
printf("Wrong Argument: %s\n", argv[1]);
usage();
}
++argv;
--argc;
}
return 0;
}
As you can see, in case of -d, this prints what's following the argument, without space; here is a sample output:
./myprogram -dfilename
Program name: myprogram
filename
filename
and with the -f parameter,
./myprogram -f filename
Program name: myprogram
ffilename
it prints twice the first letter, and i don't understand why. Could someone help?