Page 1 of 1
simple stuff--input as a variable
Posted: Mon Aug 02, 2004 11:49 pm
by tom
Code: Select all
(print "filename? ")
(device (open (read-line) "write"))
(println "#!/usr/bin/newlisp \n")
(println "; $Id$")
(print "; ")
(println (date (apply date-value (now)))"\n\n" )
(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?
Posted: Tue Aug 03, 2004 12:17 am
by Lutz
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:
Code: Select all
(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