Editing a Stream Question
Posted: Sun Oct 24, 2010 5:31 pm
This should be obvious, but apparently I'm missing it. The idea is to read in a file, do a string replace and write it back out again.
My initial code is appending the edited text to the existing text rather than overwriting the old text. Any pointers?
My initial code is appending the edited text to the existing text rather than overwriting the old text. Any pointers?
Code: Select all
(defun read-file-replace-string1 (file-name string-pattern replacement-string)
"Take a fully qualified filename, read it, replacing the string-pattern with the replacement string."
(with-open-file (stream file-name :direction :io :if-exists :overwrite)
(loop for line = (read-line stream nil)
while line do
(format stream "~a~%"
(cl-ppcre:regex-replace string-pattern line replacement-string)))))