运行程序不会打印第二个 printf 语句

发布于 2025-01-10 10:12:21 字数 1447 浏览 0 评论 0原文

每当我运行该程序时,第二个打印语句都不会打印。我尝试使用函数,但我是 C 新手,不太了解任何东西。我还附上了我的活动,因为我不确定如何执行其他操作。活动照片

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

#define LINE_LENGTH 1000

int main(int argc, char **argv)
{   
    FILE *input_file = fopen("cstest.c", "r");
    char line [LINE_LENGTH];
    
    //while loop to ger
    while (fgets(line, LINE_LENGTH, input_file) != NULL)
    {   
        int ch = 0;//ch is the cast
        int lines = 0;//start with one because 
        if (input_file == NULL) 
            return 0;
    
        while (!feof(input_file))
        {
            ch = fgetc(input_file);
            if (ch == '\n')
            {
                lines++;
            }
        }//end while
    
        printf("lines: %d\n", lines);

    }//end while loop

    while (fgets(line, LINE_LENGTH, input_file) != NULL)
    {   
        int ch = 0;//ch is the cast
        int lines = 0;//start with one because 
        if (input_file == NULL) 
            return 0;

        while (!feof(input_file))
        {
            int characters = 0; 
            char c;
    
            for (c = getc(input_file); c != EOF; c = getc(input_file))
                // Increment count for this character
                characters = characters + 1;
    
            printf("characters: %d\n", characters);

            fclose(input_file);
        }
    }
}

whenever I run the program the second print statement isn't printing. I tried using a function but I'm new to C and don't really understand anything. I've also attached my activity as I'm not sure how to do the other things on it.activity photo

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

#define LINE_LENGTH 1000

int main(int argc, char **argv)
{   
    FILE *input_file = fopen("cstest.c", "r");
    char line [LINE_LENGTH];
    
    //while loop to ger
    while (fgets(line, LINE_LENGTH, input_file) != NULL)
    {   
        int ch = 0;//ch is the cast
        int lines = 0;//start with one because 
        if (input_file == NULL) 
            return 0;
    
        while (!feof(input_file))
        {
            ch = fgetc(input_file);
            if (ch == '\n')
            {
                lines++;
            }
        }//end while
    
        printf("lines: %d\n", lines);

    }//end while loop

    while (fgets(line, LINE_LENGTH, input_file) != NULL)
    {   
        int ch = 0;//ch is the cast
        int lines = 0;//start with one because 
        if (input_file == NULL) 
            return 0;

        while (!feof(input_file))
        {
            int characters = 0; 
            char c;
    
            for (c = getc(input_file); c != EOF; c = getc(input_file))
                // Increment count for this character
                characters = characters + 1;
    
            printf("characters: %d\n", characters);

            fclose(input_file);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文