getopt switch语句永远不会达到默认大小写 [英] getopt switch statement never hitting default case

查看:95
本文介绍了getopt switch语句永远不会达到默认大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试搜索,但没有找到重复/类似的问题。



在这种情况下,为什么从不触发默认?



以下是C中的getopt()的问题。



以下是问题:

 而((c = getopt(argc,argv, i:o:))!= -1) {
switch(c){
case'i':
inFile = strdup(optarg);
休息时间;

情况为 o:
outFile = strdup(optarg);
休息时间;

默认值:
usage(); //这将打印 usage语句并干净地退出。
}
}

我的问题是,怎么打电话

  ./ fastsort abcd 

不打印用法并退出吗?

解决方案

下面的代码将搜索类似 -i hello 或/ and -o世界

  while((c = getopt(argc ,argv, i:o:)!= -1)

但是您执行的是:

  ./ fastsort abcd 

其中, getopt(argc,argv, i:o:)!= -1 不满意,因为没有传递的参数 abcd 是一个选择


I've tried searching and haven't found a duplicate/similar question.

How come "default" is never triggering in this case? Working on some previous homework assignments for a course to prepare for the fall, and I'm having an issue with getopt() in C.

Here are the particular lines in question:

while ((c = getopt(argc, argv, "i:o:")) != -1) {
        switch (c) {
            case 'i':
            inFile = strdup(optarg);
            break;

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

            default:
            usage(); //this prints the "usage" statement and exits cleanly.
        }
    }

My issue is, how come calling

./fastsort a b c d

doesn't print the usage and exit?

解决方案

The below code will search for options like -i hello or/and -o world

while((c = getopt(argc, argv, "i:o:") != -1)

However what you are executing is:

./fastsort a b c d

where getopt(argc, argv, "i:o:") != -1 is not satisfied since none of the passed argument a b c dis an option

这篇关于getopt switch语句永远不会达到默认大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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