In your case, the last line is seemingly read twice, except that it isn't. The last call to fgets
returns NULL
to indicate the the end of file has been read. You don't check that, and print the old contents of the buffer again, because the buffer wasn't updated.
It is usually better not to use feof
at all and check return values from the f...
family of functions:
fp = fopen("sample.txt", "r");
while (1) {
if (fgets(line,150, fp) == NULL) break;
i++;
printf("%3d: %s", i, line);
}
printf("%d\n",i);
The function feof
returns true after trying to read beyond the end of the file, which only happens after your last (unsuccessful) call to fgets
, which tries to read at or rather just before the end of the file. The answers in this long SO post explain more.