Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   file operations... (http://www.go4expert.com/forums/file-operations-t2568/)

ceus 16Jan2007 14:23

file operations...
 
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 16Jan2007 19:08

Re: file operations...
 
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 16Jan2007 22:29

Re: file operations...
 
You might want to cosider doing this in Perl. It will be far easier than in C/C++.

DaWei 17Jan2007 11:19

Re: file operations...
 
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 17Jan2007 11:42

Re: file operations...
 
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....


All times are GMT +5.5. The time now is 15:12.