Add check for EOF to prevent infinite loop

Fixes #299
Merge pull request #300 from lee-dohm/bug-299

Add check for EOF to prevent infinite loop