The Nyquist/XLisp OPEN function has the habit to overwrite an existing text file without warning. Here is way how the XLisp OPEN function can be used to to append text to a file instead of overwriting:
(defun append-to-file (filename &rest strings)
(when strings
(let (stream buffer)
;; first try to read the file into a buffer
(unwind-protect
(when (setq stream (open filename :direction :input))
;; if the file could be opened for reading
(do ((line (read-line stream)
(read-line stream)))
((null line))
(setq buffer (cons line buffer))))
(when stream
;; if the file was opened, close it
(close stream)
(setq stream nil)))
;; write the buffer and append the new text
(unwind-protect
(when (setq stream (open filename :direction :output))
;; if the file could be opened for writing
(when (not (endp buffer))
;; if there is text in the buffer
(setq buffer (reverse buffer))
(dolist (line buffer)
(format stream "~a~%" line)))
;; write the new text
(dolist (line strings)
(format stream "~a~%" line)))
(when stream
;; if the file was opened, close it
(close stream)
filename)))))
If no file exists where the text could be appended, then a new file will be created. The file gets automatically closed after the text has been written.
- edgar