Fixed an infinte loop bug when a file ended unexpectedly