Is it really possible to write in a text file in the middle or somewhere not in beginning or at the end? Here is my code which reads two lines from the file and I want to modify the second line and write to the file - yes overwriting.
Your code has a logic issue. gotMatch is false at start. Therefore, condition on line 16 is always false. For that reason line 17-20 are never evaluated, and gotMatch remains false. The logical purpose of gotMatch is probably to show only the first line that contains search_str. If so, mere break; of the while loop would do.
Your current program reads from one stream and writes to an another. Your question seems to be about inplace modification of an existing file. There is a mode for that, but I don't know it. Notice though that s/grep or cd/mkdir or rm/ overwrites 10 characters and inserts 1. That insertion shifts the rest of file content one character forward.
It is usually safer to create new (small, text -- not huge databases) files. If edit is successful, then you can remove original and rename the new.
Thanks a lot, any help in the form of code could be very grateful or atleast some links for to be achieved. If i rewrite the entire file to the new, I dont want my line number atleast to be changed, data can change, in place of `grep or cd`, I get `mkdir or rm`
What is the right way to rewrite my file, with modified line at that particular position? please let me know...