/*--------------------------------------------------------------------------------
File :rec28.c
Program :Program to count characters,words and lines in a text file,accept the file name through the command line.
Author :sujithbaby440@gmail.com
Date :25-11-2014
----------------------------------------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
main(int argc,char *argv[])
{
FILE *fp;
int ch,c=0,l=0,w=0;
if(argc<2){
printf("Enter proper arguments\n");
exit(0);
}
if((fp=fopen(argv[1],"r"))==NULL){
printf("Error:No Read Permission\n");
exit(0);
}
while((ch=getc(fp))!=EOF){
c++;
if((ch==' ')||(ch=='\n'))
w++;
if(ch=='\n')
l++;
}
printf("Number of characters:%d\n",c);
printf("Number of words:%d\n",w);
printf("Number of lines:%d\n",l);
fclose(fp);
}
Output 1:
[141740@localhost ~]$ ./a.out count.c
Number of characters:717
Number of words:314
Number of lines:27
Output 2:
[141740@localhost ~]$ ./a.out trace.c
Number of characters:520
Number of words:198
Number of lines:27
File :rec28.c
Program :Program to count characters,words and lines in a text file,accept the file name through the command line.
Author :sujithbaby440@gmail.com
Date :25-11-2014
----------------------------------------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
main(int argc,char *argv[])
{
FILE *fp;
int ch,c=0,l=0,w=0;
if(argc<2){
printf("Enter proper arguments\n");
exit(0);
}
if((fp=fopen(argv[1],"r"))==NULL){
printf("Error:No Read Permission\n");
exit(0);
}
while((ch=getc(fp))!=EOF){
c++;
if((ch==' ')||(ch=='\n'))
w++;
if(ch=='\n')
l++;
}
printf("Number of characters:%d\n",c);
printf("Number of words:%d\n",w);
printf("Number of lines:%d\n",l);
fclose(fp);
}
Output 1:
[141740@localhost ~]$ ./a.out count.c
Number of characters:717
Number of words:314
Number of lines:27
Output 2:
[141740@localhost ~]$ ./a.out trace.c
Number of characters:520
Number of words:198
Number of lines:27
No comments:
Post a Comment