【发布时间】: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 文件。 c、h 和 o 文件可以正常工作/计数。任何帮助表示赞赏
【问题讨论】:
标签: c command-line