This program is used to get the information about any file name entered by user when the program is executed.At the starting of the program we have l=1 because when any VALID file name is entered then it has minimum 1 lines and we have informed the program if the character "\n" is found then number of line is increased by one.so it will give right answer but if we has l-0 at starting of program then error will occur that if file contains 2 lines then it will print 1 only because "\n"is found only one time in the file containing 2 lines.Further run this program on your PC to learn more and don't worry if you can't able to understand this paragraph.
Program:
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
long c=0,l=1,w=0;
char ca,f[200];
clrscr();
printf("Enter file name: ");
scanf("%s",&f);
fp=fopen(f,"r");
ca=fgetc(fp);
while(ca!=EOF)
{
if(ca==' ')
{
w++;
}
if(ca=='\n')
{
l++;
}
c++;
ca=fgetc(fp);
}
printf("\n Lines: %ld",l);
printf("\n Characters: %ld",c);
printf("\n Words: %ld",w);
fclose(fp);
getch();
return 0;
}
Program:
#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
long c=0,l=1,w=0;
char ca,f[200];
clrscr();
printf("Enter file name: ");
scanf("%s",&f);
fp=fopen(f,"r");
ca=fgetc(fp);
while(ca!=EOF)
{
if(ca==' ')
{
w++;
}
if(ca=='\n')
{
l++;
}
c++;
ca=fgetc(fp);
}
printf("\n Lines: %ld",l);
printf("\n Characters: %ld",c);
printf("\n Words: %ld",w);
fclose(fp);
getch();
return 0;
}