There is no way to really get around haveing to rewrite the whole file. The way files work is you can either append like a log or buffer in and rewrite. Now its not like you are going to have to echo every line back to the file but if you need to add something to the middle the very basic process is read the file into memory print back all the file before where you are adding print back the new stuff, print back the rest of the file.