Another option is to use the external utility sed. It's been ported to nearly everything. If you haven't used sed, you haven't lived.

Another is to read the whole file in, replacing as you go, and then write it back out. This is probably identical to using a temp file for small files.

Jeff