file operations...

ceus's Avatar, Join Date: Oct 2006
Go4Expert Member
hello friends,
i want to search a string in a file and replace that with the other string ?? can anyone gimme a code for this....
thanks...
DaWei's Avatar, Join Date: Dec 2006
Team Leader
By far the easiest method (presuming that the replacement may be longer) is to read the file up to the string, writing it to a temp file in the process, put the replacement string in the temp file, copy the remainder to the temp file, delete the original, and rename the temp with the original name. Editing a file "in place" is not a trivial undertaking unless you get into raw file handling instead of using the OS.
ever_thus's Avatar, Join Date: Jan 2007
Contributor
You might want to cosider doing this in Perl. It will be far easier than in C/C++.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
I would say, just as a personal observation, that when a person is seeking solutions for a particular language, they are not extremely interested in being directed to use another. Difficulty in understanding a particular part of a language is not often best attacked by throwing in the towel and switching to Perl, Python, or Haskell, despite their sometimes easier approaches, via added abstractions.
ceus's Avatar, Join Date: Oct 2006
Go4Expert Member
thanks DaWei,
i have done that part what u told..i.e. copying that to a sting and replacing there and then fwrite() to the original file... but how can i do that without copying ???
and one more thing.. sorry if it is silly.. i cant see the threads started by me !!!!! what to do for this... thats y by mistake have posted the thread twice....