i want to write a program which will open a file count the no.of words,lines and character in that file taking the delimiter from the command line arguments.
here is the program
Code:
# include<stdio.h>
# include<conio.h>
main(int argc,char*argv[])
{
FILE*fp;
char str[80],ch;int i=0,l=0,len=0;
int words=0;
fp=fopen(argv[1],"r");
if(argc!=3)
{
puts("insuffiecient no. of parameters");
exit();
}
if(fp==NULL)
{
puts("unable to open the file");
exit();
}
while((ch=getc(fp))!=EOF)
{ i++;
if(ch=='\n')
{
l++;
}
}
printf("the number of characters in the file is %d \n",i);
printf("the number of lines in the file is %d\n",l);
rewind(fp);
while(fgets(str,79,fp)!=NULL)
{
len=strlen(str)-2;
for(i=0;i<len;i++)
{
if(str[i]==*argv[2]&&str[i+1]!=*argv[2])
{
words++;
}
}
words++;
}
fclose(fp);
printf("the number of words %d",words);
getch();
}
giving the delimiter as "," it gives no. of words=0 and other than , it is giving insufficient no. of arguments.
here is the text file which i am using
saurav is great.
He is the greatest of all,he is good,surely he is.
i have two commas so it should have shown the no. of words something differend,could anyone please figure out the defect in the programe.Thanks for seeing this thread.