i searched on net but not getting the exact idea of how to do it as the code m being able to write is just for a single occurence of the pattern but if there are different lines of occurence of that pattern??
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
If you are on a Unix or similar system (like Linux or MacOS X) then you already have a command line program to do this:
sed
.Otherwise you have to read from the original file and write to a new file, replacing the text while reading and writing. After that you have to rename the new file as the old original file.
As for the actual finding of the text, if it's a fixed string you can use e.g.
strstr
, other wise look into regular expressions.Edit: How to do it with
sed(1)
:The above command will read
infile.txt
, replace all occurrences of the textxyz
withabc
and write it back toinfile.txt
.Edit: How to search/replace:
This code has been tested to work.
Note: If you don't know what pointer arithmetic is, then the above code might be very hard to follow and understand, and you just have to trust me that it does what it should. :)