坏的说法在C / C ++处理 [英] Bad argument handling in C/C++

查看:148
本文介绍了坏的说法在C / C ++处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的程序,比如 ./方案A B C D 而不是

  ./程序-i INFILE -o不过outFile

它告诉我,什么是错的文件打开(这是真的),但

 预计:用法:程序-i输入文件-o输出\\ n
得到的答案:错误:无法打开文件/没有/等/档案\\ n

你知道应该怎么处理呢?任何线索?
另外这是我的code的一部分,坏参数处理涉及:

  IF((S = strrchr(argv的[0],'\\\\'))/ *获取文件名W / O扩展名为.exe * /
                  || (S = strrchr(argv的[0],'/')))
              小号++;
        其他
              S = argv的[0];
        如果(INFILE == NULL ||不过outFile == NULL){
        error_usage(多个);
         }
        如果(argc个!= 5)
           {
             error_usage(多个);
             返回-1;
           }
        而((c = getopt的(ARGC,ARGV,我:○:!))= - 1){
            开关(三){
         案例'我':
                          INFILE =的strdup(OPTARG);
                 打破;
                 案例'O':
                          不过outFile =的strdup(OPTARG);
                 打破;
                 默认:                          error_usage(多个);                      }
                }      如果(!(iFile的FOPEN =(INFILE,R +))){
      fprintf中(标准错误,错误:无法打开文件%s \\ n,INFILE);
      出口(1);
   }


解决方案

以及所有我能做的就是重复回答您的<一个href=\"http://stackoverflow.com/questions/18952077/how-to-handle-exceptions-when-we-give-no-argument-to-a-c-executable-program\">earlier问题,我认为这是正确的答案始终。

请参阅INFILE和不过outFile为NULL,那么的后看到的你getopts的循环检查,如果不是仍是NULL。如果他们然后打印用法消息并退出

  INFILE =不过outFile = NULL;
    而((c = getopt的(ARGC,ARGV,我:○:!))= - 1){
        开关(三){
        案例'我':
            INFILE =的strdup(OPTARG);
            打破;
        案例'O':
            不过outFile =的strdup(OPTARG);
            打破;
        默认:
            error_usage(多个);
        }
    }
    如果(INFILE == NULL ||不过outFile == NULL)
        error_usage(多个);

您放在INFILE和不过outFile检查在错误的地方在code。它应该去的之后的while循环。你在做什么,而循环同时设置的值,如果前面是检查 INFILE 不过outFile 和抱怨用户如果它没有。正如我以前说过,我不认为如果(ARGC!= 5)是有帮助的,我只是将其删除。

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

解决方案

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.

这篇关于坏的说法在C / C ++处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆