//统计程序本身有多少个字符,多少行,打印输出
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//统计程序本身有多少个字符,多少行,打印输出
int main()
{
FILE* file;
int num = 1;//字符
int line = 0;//行
int char_c;//fgetc返回值
file = fopen("demo.txt", "r");
if (!file) {
puts("open fail.");
return -1;
}
char_c = fgetc(file);
while (!feof(file))
{
putchar(char_c);
char_c = fgetc(file);
num++;
if (char_c == '\n' || char_c == EOF)
line++;
}
printf("有%d个字符,有%d行",num,line);
fclose(file);
return 0;
}
文本:
hello world
oh shit
handsome
my babygirl
sweet girl
结果:
hello world
oh shit
handsome
my babygirl
sweet girl有52个字符,有5行
E:\C\visualStudio\demo\statistical_char\x64\Debug\statistical_char.exe (进程 29864)已退出,代码为 0。
按任意键关闭此窗口. . .