LISP I/O Hints

use READ READ-CHAR READ-LINE for input and FORMAT for output. PRINT and PRIN1 are also available but less useful. -------------- To change between char and string data types try COERCE -------------- Steele's lisp book. Common Lisp the Language, 2nd Edition via class web page. describes OPEN and WITH-OPEN-FILE and CLOSE for file I/O "Because with-open-file always closes the file, even when an error exit is taken, it is preferred over open for most applications." ----------- (WITH-OPEN-FILE (output-stream output-file :direction :output :if-exists :new-version :if-does-not-exist :create) (WITH-OPEN-FILE (input-stream input-file :direction :input) (FORMAT output-stream "~a" (READ input-stream nil)) ) ) the value of output-file and input-file should be a string. You might be able to get away with just the file name e.g., "rules.lsp" but you might need a path "/users/csfaculty/dcb/ai/proj1/rules.lsp" Using (READ input-stream nil) will a value of NIL at end of file. Using (READ input-stream nil 'eof) will return the symbol eof at the end of the file. Note that (FORMAT t ... ) prints to terminal (i.e. to screen). Note that the :if-exists :new-version :if-does-not-exist :create stuff is optional. ------------- (WITH-OPEN-FILE (input-stream "dcb.in" :direction :input) (READ input-stream nil) ) (WITH-OPEN-FILE (output-stream "dcb.out" :direction :output) (PRINT '(a b c d) output-stream) ) ______________________________________________________________________________ Wed Sep 13 22:13:04 EDT 2000