c/c++语言开发共享当我们不向C可执行程序提供参数时如何处理exception

我有这个C程序,它应该给出如下参数:

./program -i inputFile -o outputFile

这是我相关的代码部分

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(argv[0]); } } 

这里也是error_usage函数:

 void error_usage(char *prog) { fprintf(stderr, "Usage: %s -i inputfile -o outputfilen", prog); exit(1); } 

我应该如何修改我的case语句,如果我运行我的程序如下: ./program它会给我以下错误? Usage: prog -i inputfile -o outputfile

    在调用getopt之前,请检查argc

     if ( argc == 1 ) { fprintf(stderr, "... "); return -1; } 

    请参阅inFileoutFile为NULL

    然后在你的getopts循环之后检查是否仍然是NULL。 如果是,则打印使用消息并退出

     if (inFile == NULL || outFile == NULL) error_usage(argv[0]); 

      以上就是c/c++开发分享当我们不向C可执行程序提供参数时如何处理exception相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/519709.html

      (0)
      上一篇 2020年12月5日
      下一篇 2020年12月5日

      精彩推荐