file i/o error handling, off-by-one error