-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1.c
More file actions
45 lines (45 loc) · 1.06 KB
/
p1.c
File metadata and controls
45 lines (45 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file;
char a[100];
char ch;
int lines = 0, words = 0, characters = 0;
printf("Enter the filename: ");
scanf("%s", a);
// Open the file
file = fopen(a, "r");
//if no file exists for not showing core dumped output
if (file == NULL)
{
printf(" No such file exists.\n");
exit(1);
}
// Read characters one by one from file
while ((ch = fgetc(file)) != EOF)
{
characters++;
// Count the no of lines
if (ch == '\n') {
lines++;
}
// Count the no of words
if (ch == ' ' || ch == '\n')
{
words++;
}
}
// Closing the file
fclose(file);
// last word count
if (characters > 0)
{
words++;
}
printf("\nStatistics for file: %s\n", a);
printf("Number of lines: %d\n", lines);
printf("Number of words: %d\n", words);
printf("Number of characters: %d\n", characters);
return 0;
}