"DarthBob88" <> wrote in message
news: ups.com...
>I have to go through a file and replace any occurrences of a given
> string with the desired string, like replacing "bug" with "feature".
> This is made more complicated by the fact that I have to do this with
> a lot of replacements and by the fact that some of the target strings
> are two words or more long, so I can't just break up the file at
> whitespace, commas, and periods. How's the best way to do this? I've
> thought about using strstr() to find the string and strncpy() to
> replace it, but it occurs to me that it would screw up the string to
> overwrite part of it with strncpy(). How should I do this?
>
You'll make life a lot easier for yourself if you can specify that the
search string cannot contain newlines.
Load each line. Call strstr() repeatedly to count the number of ocurrences
of each target string. Then calculate how much extra memory is required.
(You need to think what happens if one search string is a substring of
another, or contains an overlap)
Allocate another buffer of the right length, not forgetting the terminal
nul. Then do a search and replace. Probably the easiest way to do this is to
have two buffers, search one and replace into the other, iteratively until
you have done all the targets.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm