newLISP Fan Club

Forum => newLISP in the real world => Topic started by: tom on August 02, 2004, 04:49:23 PM

Title: simple stuff--input as a variable
Post by: tom on August 02, 2004, 04:49:23 PM
(print "filename? ")
(device (open (read-line) "write"))
(println "#!/usr/bin/newlisp n")
(println "; $Id$")
(print "; ")
(println (date (apply date-value (now)))"nn" )

(close (device))
(exit)


This works, however, my attempts to set or define



(device (open (read-line) "write"))



have not worked.  Something like,



(set 'a (open (read-line) "write"))



I want to check if 'a already exists, maybe manipulate the file name

later, stuff like that.  



did my question make sense?
Title:
Post by: Lutz on August 02, 2004, 05:17:28 PM
what 'device' does, is redirecting all output from print/println (console output) do a file (device takes a file handle).



perhaps you should rather do:

(set 'fileName (read-line))
(if (not (file? fileName))                      
   (set 'fle (open fileName "write"))  ;; if the file does not exiist create it
   (ser 'fle (open fileName "append"))) ;; else append to it

(write-line (read-line) fle)  ;; write stuff from the keyboard into the file
...
...
...
(close fle)


To test the existence of a file you have to take the file name rather than the handle returned from 'open', once you opened the file for "write" you would have destroyed (reset to the beginning) the file you want perhaps to append to.



Lutz



ps: not sure if I completely understand your intentions, but hope it helps