Bad argument handling in C/C++

喜欢而已 提交于 2019-12-12 06:22:08

问题


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);
   }

回答1:


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.




回答2:


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);
};


来源:https://stackoverflow.com/questions/18952763/bad-argument-handling-in-c-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!