main()
{
FILE *fp;
char c;
int count=1;
fp=fopen("D:\file.txt","r");
printf("%d ",count);
c = fgetc(fp);
while(c!=EOF)
{
if(c=='\n')
{
count++;
printf("\n%d",count);
}
putchar(c);
c=fgetc(fp);
}
fclose(fp);
}
main() { FILE *fp; char c; int count=1; fp=fopen(D:\file.txt,r); printf(%d ,count); c = fgetc(fp);
Share
You are also printing the newline you just read from the file,
Change
to
Alternativly, don’t print the newline when you print the line number,
You also have to change
to
getchar() returns an int, and EOF is a value that cannot be represented by a char.