#include <stdio.h>
#include <stdlib.h>
void readFile(char file[]) {
FILE *fp = fopen(file, "r");
char ch;
while ((ch = fgetc(fp)) != EOF)
putchar(ch);
fclose(fp);
}
int main()
{
char file[100] = "d:\\data.txt";
FILE *fp = fopen(file, "r");
if (fp == NULL) {
printf("Error open file\n");
exit(EXIT_FAILURE);
}
char ch;
int chars = 0, words = 0, lines = 0;
while ((ch = fgetc(fp)) != EOF) {
chars++;
if (ch == '\n')
lines++;
if (ch == ' ' || ch == '\t' || ch == '\n')
words++;
}
if (chars > 0) {
words++;
lines++;
}
fclose(fp);
readFile(file);
printf("\n\nTotal chars = %d\n", chars);
printf("Total words = %d\n", words);
printf("Total lines = %d\n", lines);
return 0;
}
/*
run:
c c++ c#
java python
Total chars = 20
Total words = 5
Total lines = 2
*/