【问题标题】:Command line arguments reading for flags and file names in C命令行参数读取 C 中的标志和文件名
【发布时间】:2012-09-10 08:41:38
【问题描述】:

您好,我正在做一个项目,我需要我的程序从命令行运行,并且能够读取将在程序中使用的标志和文件名。

这是我当前的代码。它在不输入任何标志的情况下编译。我不认为我的 GetArgs 有任何作用。我对那部分代码有帮助。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1024
#define IN 1 /* inside a word */ 
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */

int numInputArgs;
int idx;
void GetArgs (int argc, char **argv){

for (idx = 1; idx < 4;  idx++) {
    if (strcmp(argv[idx], "-c") == 0) {
        printf("Flag -c passed\n");
        break;
    }
    else if (strcmp(argv[idx], "-w") == 0) {
        printf("Flag -w passed\n");
        break;
    }
    else if (strcmp(argv[idx], "-l") == 0) {
        printf("Flag -l passed\n");
        break;
    }
    else if (strcmp(argv[idx], "-L") == 0) {
        printf("Flag -L passed\n");
        break;
    }
    else {
        printf("Error: unknown flag\n");
        exit(-1);
    }
}
 }// end GetArgs

void lineWordCount ( ) {

int c, nl, nw, nc, state;


    state = OUT; nl = nw = nc = 0; 
    while ((c = getchar()) != EOF) {
            ++nc;

        if (c == '\n')
            ++nl; 

        if (c == ' ' || c == '\n' || c == '\t')
            state = OUT; 

        else if (state == OUT) {
            state = IN; ++nw;
        } 
        printf("%d %d %d\n", nl, nw, nc);
    }
 }// end lineWordCount








 int main(int argc, char **argv){

GetArgs(argc, argv);
lineWordCount();
printf("Hello");

//fclose( src );
}

【问题讨论】:

  • 仅供参考:作业标签已弃用。
  • 你说得对,GetArgs 所做的只是打印参数。如果您使用的是 POSIX 机器(如 Linux 或 Mac OSX),我建议您查看getopt。如果您搜索,还有许多可用的参数解析库,例如上面提到的getopt函数在Windows中也存在。
  • 您的 GetArgs 完美地完成了应有的工作:$ gcc -o issue issue.c $ ./issue.exe a b c abc 那么,您的问题到底是什么?
  • 我需要 GetArgs 能够扫描决定最后显示什么信息的四个潜在标志。在处理文件之前。

标签: c command-line command-line-arguments word-count


【解决方案1】:

您可以使用@Joachim 提到的getopt() 之类的标准函数(如果它在您的系统上可用),或者您可以自己编写代码。如果您有复杂的命令行语法,getopt() 可能更适合 - 如果您只需要检查一组有限的标志,则自己编写代码可能更容易,例如:

void GetArgs (int argc, char **argv){
   int idx = 0;

   for (idx = 1; idx < argc;  idx++) {
       if (strcmp(argv[idx], "-a") == 0) {
          printf("Flag -a passed\n");
       } else if (strcmp(argv[idx], "-b") == 0) {
          printf("Flag -b passed\n");
       } else if (strcmp(argv[idx], "-c") == 0) {
          printf("Flag -c passed\n");
       } else {
          printf("Error: unknown flag %s\n");
       }
    }
}

【讨论】:

  • 我的主文件有问题吗.. 除了 lineWordCount 中的内容之外,它没有打印任何其他内容
  • lineWordCount() 在上面的代码中根本没有被调用。我把你的代码原封不动地传递给编译器,当我用几个命令行参数调用程序时,它会打印每个参数,这正是我所期望的......
  • 我在主要的 GetArgs 下添加了它,但我卡在了 GetArgs 中。试图弄乱它,这样它就会到达 lineWordCount。我马上更新代码。
  • 我知道原因了。我使用的是内置在“wc”(字数统计)中的 UNIX,我认为这是我的程序在运行。 :(
  • 在 unix shell 中,调用您的程序时总是在前面加上./ 以避免此类问题。另外,避免使用现有名称(您可以使用whichwhere 检查)
【解决方案2】:

我建议您使用argtable2 库。我已经使用了很长时间,我认为它很棒。有教程可以了解它的强大和易用性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 2013-05-27
    • 2018-06-06
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多