Write a program that takes a file as an argument and counts the total number of lines. Lines are defined as ending with a newline character.

#include<stdio.h>

main()
{
int c,l=0;
char str[20];
FILE *fin;
puts("Enter the file name::");
gets(str);
fin=fopen(str,"r");
if(fin==NULL)
{ printf("\nError while opening.");
exit(0);
}
else { do{c=getc(fin);
if(c=='\n') l++;
}while(c!=EOF);
fclose(fin);
printf("\nTotal number of lines:: %d\n",l);
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s