I need to search a file for a specific string, then modify the line where that string is. How can I read a file, remember my place in the file, and modify the file at that place? Thanks.
I guess what I would do is open file A for reading, and file B for writing. Read from file A, line by line - if the line doesn't contain the string you're looking for, then just write the line to file B - if the line does contain the string you're for, then write the "changed" line to file B.
You end up with file B which is the same as file A, except that the string you were looking for has been found and changed in file B.
When that loop has finished, you can "move" file B to file A - you basically want to remove the original file A, and rename file B to file A. There are at least a few ways to accomplish that.
As you're writing your code, test it along the way. If you have trouble with a specific part, post your problem and someone can help you.
Yea - I've not much experience with that sort of thing. I like that solution - very elegant - it's a utility that's available just for such situations.
That does deserve a star. Supa-star!
Actually, I'm not simply substituting one string for another. I need to do some more processing according to another input file. So I think I'll need to do what hmerril suggested the first time. Thank you both!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.