Mona Jalal
Mona Jalal

Reputation: 38145

Bad argument handling in C/C++

When I run my program like ./program a b c d instead of

./program -i inFile -o outFile

it tells me something is wrong with the file opening (which is true )but

Expected: "Usage: program -i inputfile -o outputfile\n"
Got: "Error: Cannot open file /no/such/file\n" 

Do you know how should I handle this? Any clue? Also this is part of my code which deals with bad argument handling:

if ((s= strrchr( argv[0], '\\')) /* get filename w/o .exe extension */
                  || (s= strrchr( argv[0], '/')))
              s++;
        else
              s= argv[0];
        if(inFile == NULL || outFile == NULL) {
        error_usage(s);
         }
        if ( argc !=5 )
           {
             error_usage(s);
             return -1;
           }
        while ((c = getopt(argc, argv, "i:o:")) != -1) {
            switch (c) {


         case 'i':
                          inFile = strdup(optarg);
                 break;
                 case 'o':
                          outFile = strdup(optarg);
                 break;
                 default:

                          error_usage(s);

                      }
                }

      if (!(iFile = fopen(inFile, "r+"))) {
      fprintf(stderr, "Error: Cannot open file %s\n", inFile);
      exit(1);
   }

Upvotes: 0

Views: 720

Answers (2)

john
john

Reputation: 87959

Well all I can do is repeat the answer to your earlier question, which I think was the correct answer all along.

See inFile and outFile to NULL, then after your getopts loop check to see if either is still NULL. If they are then print the usage message and exit

    inFile = outFile = NULL;
    while ((c = getopt(argc, argv, "i:o:")) != -1) {
        switch (c) {
        case 'i':
            inFile = strdup(optarg);
            break;
        case 'o':
            outFile = strdup(optarg);
            break;
        default:
            error_usage(s);
        }
    }
    if (inFile == NULL || outFile == NULL)
        error_usage(s);

You placed the check on inFile and outFile in the wrong place in your code. It should go after the while loop. What you are doing is checking if the earlier while loop sets the values of both inFile and outFile and complaining to the user if it does not. And as I said before I don't think if (argc != 5) is helpful, I would just delete it.

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 476970

Getopt doesn't know about "mandatory" arguments. It will parse all the arguments it can find, but it is your responsibility to check the higher-level logic, i.e. whether a consistent set of arguments has been supplied, etc.

Also, you don't generally need to copy strings, since the original string will always be there. The following might work just fine:

char const * infile = NULL;

// ...

case 'i':
    infile = optarg;
    break;

// ....

if (!infile)
{
    puts("Error, you must specify an input file. Use -h for help.\n");
    exit(EXIT_FAILURE);
};

Upvotes: 0

Related Questions