Yet another item urgently needed in the non-existent FAQ.
(No offense meant; I just recently realized that if I wrote the same boilerplate at the top of every such answer, they I'd be halfway to creating such a FAQ!)
There's no direct method of rewriting or inserting lines into a text file. If the file is small (compared to physical ram) you can just read the whole thing into an array, modify the desired element, and write the whole thing back. If the file is large, you might be better off reading the file line by line and counting lines. (This will be slower for you of course, but nicer to the other processes on the machine.) Either way, don't forget to implement some kind of locking protocol using flock() if it's possible for more than one user to be updating the file at the same time.