[英]How to check last character in command line arguements?
所以我试图计算命令行中的文件数,这是我到目前为止所得到的
# 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
语句。 BTW 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++;
}
}
所以现在的问题是它不能将makefile
识别为 Makefile。 它把它们算作Other
。 所以我应该说 5 个Makefile
和 10 个Other
文件,它说我有 0 个makefiles
和 15 个Other
文件。 c
、 h
和o
文件可以正常工作/计数。 任何帮助表示赞赏
您可以使用 strlen() 来确定 argv 中每个字符串的长度。 知道长度后,您应该能够在数组中的最后一个元素处达到峰值。
注意:我不会为您的家庭作业编写代码。
您可以使用字符串函数strlen
来计算字符串的长度,要使用它,您必须包含标题<string.h>
。 从中您可以检查命令行参数的每个单词的最后一个索引满足哪个条件,例如
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++;
}
您可以使用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
像这样使用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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.