Could it be that there's some terminating character in your document (or, the data you are processing) at that position? I thought of this because Teranoz tested your code, and it runs fine on his end (assuming he used a different file).

Also, a counter-test would be to change the increment (for example, increment $i by 2 in every loop iteration), and see where it stops then.