【问题标题】:How to check last character in command line arguements?如何检查命令行参数中的最后一个字符?
【发布时间】:2017-10-19 03:34:15
【问题描述】:

所以我试图在命令行中计算文件的数量,这是我到目前为止得到的结果

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

int main(int argc, char* argv[]){
int cCount = 0;
int cHeadCount = 0;
int objCount = 0; 
int makeCount = 0;
int othCount =0;

for(int i = 1; i< argc; i++){
    /*
    if argv[i] last char is c 
        cCount++;

    if argv[i] last char is h 
        cHeadCount++;

    and so on for the rest

    */
printf("C Source: %d\n", cCount);
printf("C Header: %d\n", cHeadCount);
printf("Object: %d\n", objCount);
printf("Make: %d\n", makeCount);
printf("Other: %d\n", othCount);
}

所以如果你输入类似$ ./fm main.c main.o sub.c sub.o 的东西,你应该得到

C source: 2
C header: 0
Object: 2
Make: 0
Other: 0

我需要帮助的是 for 循环中的 if 语句。顺便说一句,for循环是否正确?有没有返回字符串最后一个字符的函数?从我所见,我似乎不记得一个,但我可能是非常错误的。

如果我做错了,或者在正确的轨道上,请告诉我。任何帮助表示赞赏。

编辑:

这是我在 for 循环中的内容:

for(int i = 1; i < argc; i++){
    int len = strlen (argv[i]);

    if ((argv[i][len - 2] != '.') ){
        if((strcmp(argv[i], "Makefile")==0) || (strcmp(argv[i], "makefile")==0)){
        makeCount++;
    }else{
        othCount++;
        continue; //if i take this out, it counts `other` objects wrong too
    }
    }

    if(argv[i][len - 1] == 'c'){
        cCount++;

    }

    else if(argv[i][len - 1] == 'h'){
        cHeadCount++;

    }

    else if(argv[i][len - 1] == 'o'){
        objCount++;

    }

    else {
        othCount++;           
    }       

}

所以现在的问题是它没有将makefiles 识别为 Makefile。它将它们计为Other。所以我应该说 5 Makefiles 和 10 Other 文件,它说我有 0 makefiles 和 15 Other 文件。 cho 文件可以正常工作/计数。任何帮助表示赞赏

【问题讨论】:

  • 点赞this
  • 或者您可以使用strrchr 获取指向最后一个c, h, or o 的指针并检查!*(p+1) 以确认它是参数中的最后一个字符。
  • 如果你在当前代码中像$ ./fm Makefile一样执行(添加#include &lt;string.h&gt;),你会得到Make: 1 Other: 1。它不会变成Make: 0。另外,len-2len-1 不带保护也不好用。
  • @BLUEPIXY 所以它应该工作吗?
  • 点赞this

标签: c command-line


【解决方案1】:

像这样使用strrchr

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]){
    int cCount, cHeadCount, objCount, makeCount, othCount;
    cCount = cHeadCount = objCount = makeCount = othCount = 0;

    for(int i = 1; i < argc; i++){
        char *filename = strrchr(argv[i], '/');
        filename = filename ? filename + 1 : argv[i];

        char *ext = strrchr(filename, '.');
        if(strcmp(filename, "Makefile") == 0 || strcmp(filename, "makefile") == 0){//or use strcasecmp ?
            ++makeCount;
        } else if(ext == NULL) {
            ++othCount;
        } else if(ext[1] == 'c' && ext[2] == 0){
            ++cCount;
        } else if(ext[1] == 'h' && ext[2] == 0){
            ++cHeadCount;
        } else if(ext[1] == 'o' && ext[2] == 0){//or strcmp(ext, ".o")==0
            ++objCount;
        } else {
            ++othCount;
        }
    }
    printf("C Source: %d\n", cCount);
    printf("C Header: %d\n", cHeadCount);
    printf("Object: %d\n", objCount);
    printf("Make: %d\n", makeCount);
    printf("Other: %d\n", othCount);
}

【讨论】:

  • 谢谢。这行得通:) 你能解释一下filename = filename ? filename + 1 : argv[i]; 行在做什么吗?
  • @RobBor 如果没有/,则使用argv[i]作为文件名,存在的情况下从下一个位置开始使用。
【解决方案2】:

您可以使用 strlen() 来确定 argv 中每个字符串的长度。知道您应该能够在数组中的最后一个元素处达到峰值的长度。

注意:我不会为你的家庭作业编写代码。

【讨论】:

    【解决方案3】:

    您可以使用字符串函数strlen 来计算字符串的长度,并且要使用它,您必须包含标题&lt;string.h&gt;。从那里你可以检查命令行参数的每个单词的最后一个索引,它满足哪个条件,比如

    for(int i = 1; i< argc; i++){
            int l=strlen(argv[i]);
        if(argv[i][l-1]=='c')
            cCount++;
        else if(argv[i][l-1]=='h')
            cHeadCount++;
        else if(argv[i][l-1]=='o')
            objCount++;
        else if(argv[i][l-1]=='m')
            makeCount++;
        else othCount++;
    
    
       }
    

    【讨论】:

      【解决方案4】:

      您可以使用switch 语句来执行此操作:

      # include <stdio.h>
      # include <stdlib.h>
      # include <string.h>
      
      int main(int argc, char* argv[]){
              int cCount = 0;
              int cHeadCount = 0;
              int objCount = 0;
              int makeCount = 0;
              int othCount =0;
              int len = 0;
      
              for(int i = 1; i< argc; i++){
                      len = strlen (argv[i]);
                      /* Assuming that any argv (filename) which either 
                       * do not have any extension or extension of 
                       * more than 1 character goes under "other" file catagory
                       */
                      if (len > 2 && (argv[i][len - 2] != '.')){
                              othCount++;
                              continue;
                      }
                      switch(argv[i][len - 1]){
                              case 'h':
                                      cHeadCount++;
                              break;
                              case 'c':
                                      cCount++;
                              break;
                              case 'o':
                                      objCount++;
                              break;
                              case 'm':
                                      makeCount++;
                              break;
                              default:
                                      othCount++;
                              break;
                      }
              }
              printf("C Source: %d\n", cCount);
              printf("C Header: %d\n", cHeadCount);
              printf("Object: %d\n", objCount);
              printf("Make: %d\n", makeCount);
              printf("Other: %d\n", othCount);
              return 0;
      }
      

      程序的输出:

      $ ./fm main.c main.o sub.c sub.o
      C Source: 2
      C Header: 0
      Object: 2
      Make: 0
      Other: 0
      

      【讨论】:

      • 谢谢。 Make 的问题是参数是 makefileMakefile,而不是 .m 的文件扩展名。我将如何修改它以便将字符串识别为文件类型?我试过把if(argv[i] == "makefile") makeCount++; 之类的东西放进去,但它似乎仍然接受它为other
      • @RobBor 使用strcmp,比如if(strcmp(argv[i], "makefile") == 0),而不是if(argv[i] == "makefile")
      • @BLUEPIXY 好主意。但我没有任何运气:( 我试过把它放在if( len &gt; 2 &amp;&amp; ... 和之前,但似乎没有一个工作。
      • @RobBor 我认为你在做一些奇怪的事情。我认为它会起作用。
      • @BLUEPIXY @H.S. if((strcmp(argv[i], "Makefile")==0) || (strcmp(argv[i], "makefile")==0) ){ makeCount++; continue; } 错了吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      • 2013-10-16
      • 1970-01-01
      相关资源
      最近更新 更多