If the new line is the same length as the old one, best
- Open temporary output file
- Read the configuration file line by line
- Key Search
- If you cannot find it, just write the line you just read into the output file
- If you can find it, write the new value in a temporary file instead
- Until you hit EOF
- Delete old file
- Rename the new file to the old file
If your configuration file is small, you can also perform the entire parsing / changing step in memory, and then write the final result back to the configuration file, thereby skipping the temporary file (although the temporary file is a good way to prevent corruption if something breaks when you write a file).
If this is not what you are looking for, you should change your question to be much clearer. I just guess what you're asking for.
source share