Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C program launch argument

I want to put arguments to launch the program
For exemple : ./program -i inputfile -e -b.
I have done the code bellow but I have a problem the argument needs an option for exemple it works when I put -i inputfile but if I put -e -b it will take the -b as an option for the -e.
I haven’t found a solution to this as it asks me to enter an option for each argument

Thank you if you can help me

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

#define OPTSTR "h:b:c:d:e:f:i:l:g:o:r:x:s:Z:z"

int main(int argc, char *argv[]) {
    int opt;
    while ((opt = getopt(argc, argv, OPTSTR)) != EOF) {
        switch(opt) {
            case 'h':
                printf("AIDE POUR LE PROGRAMME");
                break;
            case 'b':
                printf("-b ENTERED");
                break;
            case 'c':
                printf("-c ENTERED");
                break;
            case 'd':
                printf("-d ENTERED");
                break;
            case 'e':
                printf("-e ENTERED");
                break;
            case 'f':
                printf("-f ENTERED");
                break;
            case 'i':
                printf("-i ENTERED");
                break;
            case 'l':
                printf("-l ENTERED");
                break;
            case 'g':
                printf("-g ENTERED");
                break;
            case 'o':
                printf("-o ENTERED");
                break;
            case 'r':
                printf("-r ENTERED");
                break;
            case 'x':
                printf("-x ENTERED");
                break;
            case 's':
                printf("-s ENTERED");
                break;
            case 'Z':
                printf("-Z ENTERED");
                break;
            case 'z':
                printf("-z ENTERED");
                break;
            default:
                printf("Mauvais argument entré ! \n");
                break;
        }
    }
    return 0;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In your option string, a letter followed by a : means that option expects an argument. If an option does not take an argument, don’t put : after it.

#define OPTSTR "h:b:c:d:ef:i:l:g:o:r:x:s:Z:z"
//                       ^-- no colon after "e"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading