Excluding the very special case where changes result in no change to the length (or encoding) of a word, you basically have to
- read in each line in turn.
- decide if you need to change the line in some way.
- write out the (possibly modified) line to a new temporary file.
When you're done, you can then decide to
- rename the original file to say 'file.bak', then rename the temporary to file.txt
- Forego the backup, and just replace file.txt with the newly created temporary.