in reply to Inserting new column

You cannot (should not) write to the open file while you are still reading from it. Open a new tempfile, write to that, then once the whole file has been read and written, close both and rename the tempfile as the original file (taking a backup first to be prudent). The alternative is to read the whole file in first, store the results in RAM and then write them all out in one go. HTH.