要求:编写一个程序,统计该程序本身一共有多少个字符,有多少行,并打印输出。
#include <stdio.h>
int main()
{
FILE *file; //文件指针
char c; //存储读取到的字符
int words=0; //记录字符个数
int lines=0; //记录行数
//打开文件,如果打开失败会返回NULL(相当于0)
file=fopen("hello.txt","r");
if(!file)
{
printf("文件打开失败!");
return 1;
}
//统计字符个数和行数
//fgetc()读取失败或到文件末尾会返回EOF(就是-1)
while((c=fgetc(file))!=EOF)
{
if(c!='\n')
{
words++;
}
else
{
lines++;
}
}
//因为文件末尾一般没有换行符号,因此可能少一行
lines++;
printf("%d\n",words);
printf("%d\n",lines);
//记得关闭文件
fclose(file);
return 0;
}