编程语言
首页 > 编程语言> > 下面程序用变量count统计文件中字符的个数

下面程序用变量count统计文件中字符的个数

作者:互联网

下面程序用变量count统计文件中字符的个数

#include <stdio.h>
// 下面程序用变量count统计文件中字符的个数

int main()
{
    FILE *fp;
    long count = 0;
    if ((fp = fopen("letter.dat", "r")) == NULL) // 1
    {
        printf(" cannot open file\n");
        // exit(0);
    }
    while (!feof(fp)) //其功能是检测流上的文件结束符,如果文件结束,则返回非0值,否则返回0
    {
        fgetc(fp); //用于读取文件中的一个字符 2
        count++;
    }
    printf("count = %ld\n", count);
    fclose(fp);

    return 0;
}

letter.dat:

12345

result:

注意文件结尾还有一个结束字符 '\0’

count = 6

fgetc意为从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节
格式:int fgetc(FILE *stream);

标签:count,字符,读取,文件,fgetc,个数,fp
来源: https://blog.csdn.net/qq_44880154/article/details/110495813