twitter
    Find out what I'm doing, Follow Me :)

This Is A Problem

Inspection of the program will reveal that when we read data in and detect the EOF, we print out something before we check for the EOF resulting in an extra line of printout. What we usually print out is the same thing printed on the prior pass through the loop because it is still in the buffer "oneword". We therefore must check for EOF before we execute the "printf" function. This has been done in readgood.c, which you will shortly examine, compile, and execute.

Compile and execute the original program we have been studying, readtext.c and observe the output. If you haven’t changed TENLINES.TXT you will end up with "Additional" and "lines." on two separate lines with an extra "lines." displayed because of the "printf" before checking for EOF.

Compile and execute readgood.c and observe that the extra "lines." does not get displayed because of the extra check for the EOF in the middle of the loop. This was also the problem referred to when we looked at readchar.c, but I chose not to expound on it there because the error in the output was not so obvious.