Both of the formatex() functions where you use %i, you do it incorrectly. You must specify the variable that you want inserted into that place holder. I'm surprised you are not getting run-time errors for this.
Where exactly do you have this issue? There are several place where you try to write to a file. If it's all of them, I recommend that you create a small test plugin that simply writes to a file with the method that you are trying to do. I would start with not doing any formatting and simply write some hard coded text first. Then, if that works, add in some formatting and see if it works. If not, something is going wrong with the formatting.