in reply to write to/append a specific line in output file
Your idea of putting the different sets into different files and then concatenating them together isn't as bad as you seem to think.
If you're not storing it in ram, you've got to put it on disk somehow, and that seems like the most straight-forward simple solution.
Having said that, what are you doing that requires this strange file format? Often when someone asks a question like this it turns out that there is a better way once we know more about the overall situation. Of course, often it also turns out that you just need a quick fix and you don't want to hash out the whole project, which I totally understand.