^ Non sequitur.
WaltP misread (or did not fully read) the original code, which would simply not accomplish the task. It was the wrong structure, perhaps of the "git-er-done" type, I don't know.
It basically said
* Do all input
* Do all output
whereas the natural structure for the task is
* Open input and output files
* Process files
* Close files
It was certainly not my intent that all of that logic was to be implemented inline. I guess that's what WaltP was talking about. I suppose it does look like I meant it all to be inline, which I didn't really mean. I just meant to indicate a general algorithm that would accomplish the task.
It is left as a (further) exercise to the OP to divide it into appropriate functions. :)