in reply to Format file

ikegami pointed out your errors. Here are some more tips/links:
Don't fall into the trap of using a "printf" when a simple "print" would do. The "print" is more efficient and less error prone.
I also noticed you are opening the same filename $ARGV[0] for both input and output. You probably don't want to do that.