C Program To Count Number of Words,Lines Characters In A File
C Program :
/* Aim: Write a program to accept a file name as command line argument and count the number of words,lines and characters present in th file. */ #include<stdio.h> #include<stdlib.h> #include<ctype.h> void main(int argc,char *argv[]) { FILE *fp; char ch,fname[100]; int word=0,lines=0,Char=0; if(fp==NULL) { printf("\n File does not exist \n \n"); exit(0); } fp=fopen(argv[1],"r"); while(!feof(fp)) { ch=fgetc(fp); if(isspace(ch)) word++; if(ch=='\n') lines++; Char++; } printf("\n %d words are present in the file. \n",word); printf("\n %d lines are present in the file. \n",lines); printf("\n %d characters are present in the file. \n \n",Char); }
/* Output of above code:-
[root@ugilinux ~]# cc e18a1.c
[root@ugilinux ~]# ./a.out e18a2.c
182 words are present in the file.
50 lines are present in the file.
959 characters are present in the file.
*/
Comments