file operations...

Go4Expert Member
16Jan2007,14:23   #1
ceus's Avatar
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...
Team Leader
16Jan2007,19:08   #2
DaWei's Avatar
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.
Contributor
16Jan2007,22:29   #3
ever_thus's Avatar
You might want to cosider doing this in Perl. It will be far easier than in C/C++.
Team Leader
17Jan2007,11:19   #4
DaWei's Avatar
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.
Go4Expert Member
17Jan2007,11:42   #5
ceus's Avatar
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....