Forums Assignment Help How can I open files mentioned on the command line, and parse option flags?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1246
    Aakanksha
    Participant

    How can I open files mentioned on the command line, and parse option flags?

    #9810
    Aakanksha
    Participant

    Here is a skeleton which implements a traditional Unix-style argv parse, handling option flags beginning with -, and optional filenames. (The two flags accepted by this example are -a and -b; -b takes an argument.)

    #include <stdio.h>

    #include <string.h>

    #include <errno.h>

    main(int argc, char *argv[])

    {

    int argi;

    int aflag = 0;

    char *bval = NULL;

    for(argi = 1; argi < argc && argv[argi][0] == ‘-‘; argi++) {

    char *p;

    for(p = &argv[argi][1]; *p != ”; p++) {

    switch(*p) {

    case ‘a’:

    aflag = 1;

    printf(“-a seenn”);

    break;

    case ‘b’:

    bval = argv[++argi];

    printf(“-b seen (“%s”)n”, bval);

    break;

    default:

    fprintf(stderr,

    “unknown option -%cn”, *p);

    }

    }

    }

    if(argi >= argc) {

    /* no filename arguments; process stdin */

    printf(“processing standard inputn”);

    } else {

    /* process filename arguments */

    for(; argi < argc; argi++) {

    FILE *ifp = fopen(argv[argi], “r”);

    if(ifp == NULL) {

    fprintf(stderr, “can’t open %s: %sn”,

    argv[argi], strerror(errno));

    continue;

    }

    printf(“processing %sn”, argv[argi]);

    fclose(ifp);

    }

    }

    return 0;

    }

    (This code assumes that fopen sets errno when it fails, which is not guaranteed, but usually works, and makes error messages much more useful.

    There are several canned functions available for doing command line parsing in a standard way; the most popular one is getopt.

    Here is the above example, rewritten to use getopt:

    extern char *optarg;

    extern int optind;

    main(int argc, char *argv[])

    {

    int aflag = 0;

    char *bval = NULL;

    int c;

    while((c = getopt(argc, argv, “ab:”)) != -1)

    switch(c) {

    case ‘a’:

    aflag = 1;

    printf(“-a seenn”);

    break;

    case ‘b’:

    bval = optarg;

    printf(“-b seen (“%s”)n”, bval);

    break;

    }

    if(optind >= argc) {

    /* no filename arguments; process stdin */

    printf(“processing standard inputn”);

    } else {

    /* process filename arguments */

    for(; optind < argc; optind++) {

    FILE *ifp = fopen(argv[optind], “r”);

    if(ifp == NULL) {

    fprintf(stderr, “can’t open %s: %sn”,

    argv[optind], strerror(errno));

    continue;

    }

    printf(“processing %sn”, argv[optind]);

    fclose(ifp);

    }

    }

    return 0;

    }

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.