in reply to Mixed-up output

You need to seek back to the beginning of your file before reading it the second time.