-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwc.c
48 lines (42 loc) · 772 Bytes
/
wc.c
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
46
47
48
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
int main(int argc , char *argv[])
{
int character_count = 0;
int space_count = 0;
int word_count = 0;
int line_count = 0;
char ch;
FILE *fp;
if(argc == 0)
{
printf("Please specify the filename as argument\n");
_exit(0);
}
if(fp != 0)
{
printf("No such file or directory\n");
_exit(0);
}
fp = fopen(argv[1],"r");
while((ch=fgetc(fp))!=EOF)
{
character_count++;
if(ch == ' ')
{
space_count++;
word_count++;
}
if(ch == '\n')
{
line_count++;
}
}
printf("character_count = %d\n",character_count);
printf("space_count = %d\n",space_count);
printf("word_count = %d\n",word_count+1);
printf("line_count = %d\n",line_count);
}