string substitutions within a file
In a Linux C program I'm working on, there's a part where I need to substitute a string within a file with another string; the data in the file after the new string should either be moved back or moved forward, depending on whether the new string is longer or shorter than the previous one.
An example of what I'm referring to is if you have a particular web page pulled up and you save that page to disk; each of the files within that page are saved in a subdirectory, and inside the HTML file, the new pathname for each of those files replaces the original pathname, i.e., "http://www.aol.com/script.com" becomes "aol_files/script.com".
There doesn't seem to be a real efficient way to do something like this. I was thinking that since these substitutions are within a file, I could use 'mmap' and bring the file contents into memory, make the desired changes and 'munmap' it. But that wouldn't make the string modifications any more straight-forward. I thought I'd ask to see if anyone had any suggestions of ways to go about this that I hadn't thought of (other than the cumbersome method of moving the data, char-by-char, to where it's supposed to be. Thank you in advance.