【C】---- T8:统计文件里的字符数

题目

设计一个程序,统计在读到文件结尾之前读取的字符数

编程

#include <stdio.h>

int main(void)
{
    FILE *file;
    char FileName[] = "text.txt";
    int ch, num = 0;

    //打开文件
    file = fopen(FileName, "r");
    //检查文件是否打开
    if (file == NULL)
    {
        printf("file open error\n");
        return 1;
    }
    
    while ((ch = fgetc(file)) != EOF)
    {
        num++;
    }
    printf("the char num is:%d\n", num);

    fclose(file);   //关闭文件
    return 0;
}

效果

当文件内容如下所示:
image
程序运行结果为:
image

posted @ 2024-05-04 14:59  晚风也温柔  阅读(2)  评论(0编辑  收藏  举报