Apr 24, 2013

C program to read a file and display its contents along with line numbers before each line.


#include<stdio.h>
void main()
{
FILE *fp;
char ch;
char source[67];
int count = 1;
clrscr();
puts("enter the file name:");
gets(source);
fp=fopen(source,"r");// read only mode for the source file
if(fp==NULL)
{
puts("unable to open the file:");
getch();
exit();
}
clrscr();
printf("file name:%s",source);
printf("\n line:-%d\t",count);
while((ch=getc(fp))!=EOF)
{
if(ch=='\n')
{
count++;
printf("\nline:-%d\t",count);
}
else
{
printf("%c",ch);
}
}
printf("\n press any key...");
getch();
fclose(fp);
}

0 comments:

Post a Comment