【问题标题】:Cant seem to access multiple options using getopt() for C [closed]似乎无法使用 C 的 getopt() 访问多个选项 [关闭]
【发布时间】:2019-08-15 12:02:17
【问题描述】:

TL;DR - 我的问题是我似乎无法同时使用这两个选项。只有“-n”在工作。我也希望 '-h' 工作。

我正在尝试创建一个程序,该程序基本上可以打印出“.txt”或“.log”文件的最后几个字母。但是,我在使用 getopt() 时遇到了问题。我正在尝试使用命令行访问不同的案例,但我只能访问第一个案例

我已经尝试在“nLh”之后包含冒号 (:),但它最终会输出“分段错误”(核心转储)”错误。

Ex1:./print.out -h(失败)

我传入的内容

./print.out -h

预期输出

用法:./print.out -n

实际输出

分段错误(核心转储)

Ex2:./print.out -n 60(成功)

我传入的内容

./print.out -n 60

预期输出

txt 文件中的随机文本文件 ... txt 文件中的随机文本文件

实际输出

txt 文件中的随机文本文件 ... txt 文件中的随机文本文件

    if(argc >1)
    {   
        while ((option =getopt(argc,argv,"nLh"))!=-1)
        {
            switch (option)
            {
                case 'n':

                    if( isExtensionTXTorLog && charactersRead >0)
                    {
                    }

                    else if( argc == 3 && !isExtensionTXTorLog)
                    {   
                    }
                    else
                    {
                        exit(2);
                    }
                    break;
                case 'L':
                    break;
                case 'h':
                    printUsage();
                    break;
                case '?':
                    exit(0);
                    break;
                default:
                    break;
            }
        }

    }
    else
    {
        accessDefault(buffer);
        return 0;
    }

【问题讨论】:

  • @JUSHJUSH 我更新了我的帖子。我的问题是我似乎无法让这两个选项都起作用,只有“-n”起作用。我也希望 '-h' 工作。
  • 您是否尝试过使用调试器单步执行您的程序?
  • @AndrewHenle 抱歉,我是 C 新手。到目前为止,我已经尝试过 valgrind 和 GDB 没有错误。我从 GDB 得到的只是 [Inferior 1 (process 6483) exited with code 2]

标签: c linux getopt


【解决方案1】:

您以错误的方式使用 optind。 optind 用于在解析所有选项后获取非选项参数。要解析带有参数的选项,请使用n:,然后读取 optarg 变量

看看这个最小的例子:

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    char option;
    int n_param;

    while ((option =getopt(argc,argv,"n:h"))!=-1)
    {
        //Variable initialization
        switch (option)
        {
            case 'n':
                n_param = atoi(optarg);
                printf("Param N: %d\n", n_param);
                break;
            case 'h':
                printf("Help\n");
                exit(0);
                break;
            case '?':
                printf("Unrecognized option\n");
                exit(0);
                break;
            default:
                break;
        }
    }

    for (int index = optind; index < argc; index++)
        printf ("Non-option argument %s\n", argv[index]);

    return 0;
}

例子:

./a.out ARG1 -n 50 ARG2  

输出:

Param N: 50
Non-option argument ARG1
Non-option argument ARG2

【讨论】:

  • 我的理解正确吗?我想我一定对 optarg 用于某种解析器的 optind 感到困惑。
  • 另外,如果我使用 optarg,在某个选项之后如何获取参数?例如,我想在选项 '-n' 之后获取参数,我可以使用 argv[optarg] 吗?抱歉,这是个愚蠢的问题。
  • @AlfieTorres Optarg 每次使用getopt 函数后都会设置。如果您将 'n:' 参数传递给 getopt,则 optarg 将在 case n: 中设置。
  • 谢谢,你帮了我很多忙!
猜你喜欢
  • 2021-07-26
  • 2018-09-03
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
相关资源
最近更新 更多