write cursor to file after each iteration