Fix infinite loop when reading a file