Options utilisant getOPT en C
-
29-10-2019 - |
Question
Je veux mettre une sous-option dans une chaîne afin que je puisse l'utiliser comme nom de fichier pour lire un fichier:
char *nvalue = NULL;
char *dvalue = NULL;
char *input = NULL;
char inputfilename[] = "\"";
int ar;
int main(int argc, char *argv[])
{
while ((ar = getopt(argc, argv, "hn:d:i:")) != -1)
switch (ar)
{
case 'h':
printf("something");
break; /* added */
case 'n':
nvalue = optarg;
if (isdigit(nvalue))
stop = atoi(nvalue);
else
printf("something\n");
break; /* added */
case 'd':
dvalue = optarg;
if (!strcmp(dvalue, "FCFS") || !strcmp(dvalue, "SSTF") ||
!strcmp(dvalue, "C-SCAN") || !strcmp(dvalue, "LOOK"))
;
else
printf("Invalid type of disk scheduling policy entered.\n");
break; /* added */
case 'i':
input = optarg;
strcpy(inputfilename, optarg);
printf("Filename :%s\n", inputfilename);
break;
}
/* ... */
}
Donc sur la ligne de commande si j'entre:
./foobar -i hello
Ensuite, je devrais pouvoir lire le fichier avec:
FILE *file = fopen(inputfilename, "r" );
Aucune suggestion? réponses? Merci!
Pas de solution correcte
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow