I understand that fgets reads until EOF or a newline.
I wrote a sample code to read lines from a file and I observed that the fgets gets executed more than the desired number of times. It is a very simple file with exactly two blank lines, ie. I pressed enter once and saved the file.
Below is the code:
fp = fopen("sample.txt","r");
while (!feof(fp)) {
fgets(line,150,fp);
i++;
printf("%s",line);
}
printf("%d",i);
Why is the while loop getting executed three times instead of 2, as there are only two blank lines in the file?
od -c sample.txt
. – Fancywork