【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;
}
效果
当文件内容如下所示:
程序运行结果为:
本文来自博客园,作者:晚风也温柔,转载请注明原文链接:https://www.cnblogs.com/zxr-blog/p/18172323