Search code examples
lispcommon-lispclisp

How to create and write to file without overwrite (just adding) in clisp


i want to use if-exits but dont know how to,add gives error, -when i try overwrite it changes the file

(defun writeToFile (filename content)
  (with-open-file (stream  filename :external-format charset:iso-8859-1
                           :direction :output
                           ;if-exists :add
                           :if-does-not-exist :create )
    (format stream content)
    (terpri stream)))
(loop for i from x to y
     do (if (= (is_me i) 0)
            (format t "i = ~d ~%" i)
            (writeToFile  "/home/out.txt"
              (concatenate 'string  (write-to-string i) " is me" )))
     do (if (ime i)
            (format t "~d IS ME~%" i)
            (writeToFile  "/home/out.txt"
              (concatenate 'string  (write-to-string i) " is me" ))))

Solution

  • Quick answer, you need to use :if-exists :append.

    The Common Lisp HyperSpec has the following to say about open:

    if-exists---one of :error, :new-version, :rename, :rename-and-delete, :overwrite, :append, :supersede, or nil. The default is :new-version if the version component of filespec is :newest, or :error otherwise.

    And if we look at what it says about :append:

    :append

    Output operations on the stream destructively modify the existing file. The file pointer is initially positioned at the end of the file. If direction is :io, the file is opened in a bidirectional mode that allows both reading and writing.