I would be willing to bet there's a way to accomplish this using fseek()...however, providing the files you are editing are rather small, I would read the entire file into an array, search that array line-by-line for the place you want to insert code, rewrite that line of the array, then open the file again, and print the contents of the array into it.