C 从文件中计数行、字符或字的程序

C 从文件中计数行、字符或字的程序,c,C,我写了一个程序来计算单词作为练习,但我遇到了一个问题,无论我选择哪个选项,它都计算不正确 #include <stdio.h> #include <stdbool.h> #include <string.h> int main(int argc, char **argv){ int totalcount = 0; //hold overall count for(int i = 2; i < argc; i++){ in

我写了一个程序来计算单词作为练习,但我遇到了一个问题,无论我选择哪个选项,它都计算不正确

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

int main(int argc, char **argv){
    int totalcount = 0; //hold overall count
    for(int i = 2; i < argc; i++){
        int count = 0; //hold count for each file
        int c; //temporarily hold char from file 
        FILE *file = fopen(argv[i], "r");

            if (strcmp("-c",argv[1])){
                while((c = fgetc(file)) != EOF){
                    count++;
                }
            }
            else if(strcmp("-w",argv[1])){
                bool toggle = false; //keeps track whether the next space or     line indicates a word
                while((c = fgetc(file)) != EOF){
                    if(!toggle && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))){
                        toggle = true;
                    }
                    if(toggle && ((c == '\n') || (c == ' '))){
                        count++;
                        toggle = false; 
                    }
                }
            }
            else{
                while((c = fgetc(file)) != EOF){
                    if(c == '\n'){
                        count++;
                }
            }
        }
        printf("%d %s", count, argv[i]);
        fclose(file);
        totalcount += count;
    }
    if (argc > 3){
        printf("%d total", totalcount);
    }

    return 0;
}
#包括
#包括
#包括
int main(int argc,字符**argv){
int totalcount=0;//保留总计数
对于(int i=2;i='a'&&c='a'&&c 3){
printf(“%d总计”,totalcount);
}
返回0;
}

我不知道为什么我的字符计数逻辑不起作用。我在编写每一节时都会运行我的逻辑,但我觉得它不起作用的原因没有任何意义。如果有任何帮助,我将不胜感激。

strcmp在字符串相等时返回
0
,因此永远不要进入
if/else语句

if (strcmp("-c",argv[1]) == 0){ //return value is 0
    while((c = fgetc(file)) != EOF){
        count++;
    }
}
else if(strcmp("-w",argv[1]) == 0){ //return value is 0
    bool toggle = false; //keeps track whether the next space or     line indicates a word
    while((c = fgetc(file)) != EOF){
        if(!toggle && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))){
            toggle = true;
        }
        if(toggle && ((c == '\n') || (c == ' '))){
            count++;
            toggle = false; 
        }
    }
}
if(strcmp(“-c”,argv[1])==0{//返回值为0
而((c=fgetc(文件))!=EOF){
计数++;
}
}
else如果(strcmp(“-w”,argv[1])==0{//返回值为0
bool toggle=false;//跟踪下一个空格或行是否表示单词
而((c=fgetc(文件))!=EOF){

如果(!toggle&&((c>='a'&&c='a'&&c您可以逐行读取文件,这可能会简化任务

int get_lines_chars(const char *path)
{
    /* Open templorary file */
    FILE *fp = fopen(path, "r");
    if (fp != NULL) 
    {
        ssize_t read;
        size_t len = 0;
        char *line = NULL;
        unsigned int line_no, char_no;
        line_no = char_no = 0;

        /* Read line-by-line */
        while ((read = getline(&line, &len, fp)) != -1) 
        {
            int curr_line = 0;
            while (*line)
            {
                curr_line++;
                char_no++;
                line++;
            }

            line -= curr_line;
            line_no++;
        }

        /* Cleanup */
        fclose(fp);
        if(line) free(line);

        printf("File has %d lines and %d chars\n", line_no, char_no);
        return 1;
    }

    return 0;
}

你知道 StrucMp()/Cuth>在一个匹配项上返回0吗?考虑当你(a)需要两个以上嵌套或(b)有超过20行长的函数时,将代码拆分为单独的函数。调试失败。