simple stuff--input as a variable

Q&A's, tips, howto's
Locked
tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

simple stuff--input as a variable

Post 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?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Locked