-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc.c
60 lines (53 loc) · 1.23 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
//this program countes words, lines, characters in the files that has been passed as arguments to the program in the command-line
#include <stdio.h>
#include <stdlib.h>
int main ( int argc , char* argv [ ] )
{
FILE* fp=NULL;
int nfiles =--argc;
int argidx =1;
char* currfile="";
char c;
unsigned long nw=0,nl=0,nc=0;
char t;
if(nfiles==0)
{
fp=stdin;
nfiles++;
}
else
{
currfile=argv[argidx++];
fp=fopen(currfile,"r");
}
while(nfiles>0)
{
if(fp==NULL)
{
fprintf(stderr,"Unable to open input\n");
exit(-1);
}
nc=nw=nl=0;
while((c=getc(fp))!=EOF)
{
nc++;
if (c =='\n') {
nl++;
}
if (c == ' ' && (t != ' ' && t != '\n' && t != '\t')){
nw++;
t=c;
}else if (c != ' ' && c != '\n' && c != '\t') {
t=c;
}
}
printf("%ld, %ld, %ld, %s\n",nc, nl+1, nw+1, currfile);
nfiles--;
if(nfiles>0)
{
currfile=argv[argidx++];
fp =fopen(currfile,"r");
}
}
return 0;
}