Sure, that's a good idea, so the next one is yours then. About your code, a few things...
I tried to compile this using g++ in unix, and I got a few errors, one about how the main routine should return int rather than void (which is not major as most compilers will ignore that), but a more substantive one about this line:
f.seekp(f.tellg()-i_max); //move file-write pointer back to start of string
Apparently ISO C++ forbids implicit casting of int to streampos. I wrote in an explicit cast on i_max, and after that it compiled fine. (I am actually not very familiar with C++ I/O conventions, so I don't know if this is typical, or what was going on.)
In addition, your code will correctly overwrite the matched text, but it will also continue to overwrite any other data as well. For example, suppose I had a file containing this string of text:
hi, nice to see you
and suppose I ran your program with "hi" as par2 and "gotcha" as par3 (I tried it so I know this is true). Then, your program will happily replace "hi", but it will also (unexpectedly) replace some of the characters after hi also, to make room. The final string would look like:
gotchace to see you
See? It would overwrite the comma, and the beginning of the word 'nice'. I was thinking more along the lines of having the program *insert* the replaced text rather than overwriting whatever came after, so that the replaced string should look like:
gotcha, nice to see you
but perhaps I was not clear in my requirements.
Either way, this was fun! What's the next one?
Edited by Swandog46, 04 November 2005 - 10:20 PM.