Prog#227: word count


#1
/*
word count
Program#227
*/
#include<stdio.h>
main(int argc,char *argv[])
{
int p,i,j,w,c,l,k,n;
char a[100];
if(argc!=2)
{
printf("error: no.of arg");
return 0;
}
p=open(argv[1], 0);
if(p==-1)
{
printf("file open error");
return 0;
}
w=0;c=0;l=0;
for(j=0,n=0;(i=read(p,&a[j],1))>0;j++)
{
if(a[j]=='\n')
{
l++;
a[j]='\0';
puts(a);
k=0;
while(a[k])
{
if(((a[k]!='\t')&&(a[k+1]=='\t')) || ((a[k]!=' ')&&(a[k+1]==' ')))
w++;
c++;
k++;
}
j=-1;
}
}
printf("%d %d %d", w,c,l);
close(p);
}