9

I want to know, how to create and write text file in lisp. I just want to write simple line like:

"break 1"
"break 2"

I am using LispWorks IDE on Window 7

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Nilesh Pethani
  • 822
  • 2
  • 10
  • 19

1 Answers1

17
(with-open-file (str "/.../filename.txt"
                     :direction :output
                     :if-exists :supersede
                     :if-does-not-exist :create)
  (format str "write anything ~%"))

You may also choose different settings for the with-open-file macro. If you use :append instead of :supersede then you can write into the text file while preserving its context instead of superseding the available content.

Svante
  • 50,694
  • 11
  • 78
  • 122
jkt
  • 2,538
  • 3
  • 26
  • 28
  • Thanks for the reply I am able to create and write into the file, but i cannot able to append into the file. I have change :if-exists to :append but no help, it is display the error "file already exist". You may suggest something... Thank You for help – Nilesh Pethani Feb 29 '12 at 09:39
  • you are supposed to use :if-exists :append not just :append if I understood it right. You can also check this : http://psg.com/~dlamkins/sl/chapter19.html It describes other options like overwrite, etc. – jkt Feb 29 '12 at 16:39
  • 2
    What's the '/.../' for? Does that mean './filename.txt', or just 'filename.txt' – Clayton Stanley Mar 01 '12 at 16:01
  • @claytontstanley , the first one. you can write into a file in any directory in your machine. sorry for the confusion. – jkt Mar 01 '12 at 18:13
  • @YBE; no problem; just wanting a bit of clarification on the post; thanks – Clayton Stanley Mar 01 '12 at 23:08