J'essaye d'analyser les arguments dans la ligne de commande en utilisant switch
et getopt()
. La structure est très facile: j'ai un makefile
, un .c
et un fichier .h
. C'est la première fois que j'utilise switch, donc je peux faire quelques erreurs basiques. J'ai utilisé ce lien comme guide pour passer link text et link text Si vous voyez des erreurs de base s'il vous plaît faites le moi savoir.instruction switch et fonction getopt() dans C
makefile:
make:lunar
lunar: lunar.o
gcc -Wall -std=c99 -g -o lunar lunar.o -lm
lunar.o: lunar.c lunar.h
gcc -Wall -std=c99 -g -c lunar.c
clean:
-rm -f *.o lunar core
/////////////////////////////////////
lunar.c
int main (int argc, char *argv[]){
int i;
int c = 0;
int gravity = 0;
int thrust = 0;
opterr = 0;
while ((c = getopt (argc, argv, "gtf:")) != -1)
switch (c){
case 'g':
gravity = argv[optind];
break;
case 't':
thrust = argv[optind];
break;
case 'f':
argument = argv[optind];
break;
case '?':
if (optopt == 'c')
fprintf(stderr, "Option -%c requires
an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option
`-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option
character `\\x%x'.\n",
optopt);
return 1;
defult:
abort();
}
printf ("gravity is %d and thrust is %d.\n",
gravity, thrust);
for (int index = optind ; index < argc ; index++){
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
}
///////////////////////////////////
lunar.h
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <assert.h>
#define MAX_WORD 256
Merci. Je l'ai. –