Write a program to read a file and display contents with its line numbers.
#include "stdio.h"
void main()
{
FILE *fp;
char ch;
int c=1;
fp=fopen("c:\\ctxt\\12ca.txt","r");
if(fp==NULL)
{
puts("File cannot be opened");
exit();
}
clrscr();
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
if(ch=='\n')
c++;
printf("%c",ch);
}
printf("\nThe number of lines is %d",c);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment