1 / 18

PRACTICAL COMMON LISP

PRACTICAL COMMON LISP. Peter Seibel http://www.gigamonkeys.com/book/. CHAPTER 14 FILES AND FILE I/O. READING FILE DATA. The most basic file I/O task is to read the contents of a file. OPEN function can be used to obtain a stream from which we can read a file's contents.

Download Presentation

PRACTICAL COMMON LISP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PRACTICAL COMMON LISP Peter Seibel http://www.gigamonkeys.com/book/

  2. CHAPTER 14FILES AND FILE I/O

  3. READING FILE DATA • The most basic file I/O task is to read the contents of a file. • OPEN function can be used to obtain a stream from which we can read a file's contents. • By default OPEN returns a character-based input stream we can pass to a variety of functions that read one or more characters of text: • READ-CHAR reads a single character; • READ-LINE reads a line of text, returning it as a string with the end-of-line character(s) removed; and • READ reads a single s-expression, returning a Lisp object. • We can close a file with the CLOSE function. • For examples: • Assuming that /some/file/name.txt is a file, we can open it: (open "c:/CLISP/name.txt") (let ((in (open "c:/CLISP/name.txt"))) (format t "~a~%" (read-line in)) (close in))

  4. READING FILE DATA • Of course, a number of things can go wrong while trying to open and read from a file. • The file may not exist. • We may unexpectedly hit the end of the file while reading. (let ((in (open "c:/CLISP/name.txt" :if-does-not-exist nil))) (when in (format t "~a~%" (read-line in)) (close in))) (let ((in (open "c:/CLISP/name.txt" :if-does-not-exist nil))) (when in (loop for line = (read-line in nil) while line do (format t "~a~%" line)) (close in)))

  5. READING FILE DATA • The reading functions—READ-CHAR, READ-LINE, and READ—all take an optional argument, which defaults to true, that specifies whether they should signal an error if they're called at the end of the file. • Of the three text-reading functions, READ is unique to Lisp. Each time it's called, it reads a single s-expression, skipping whitespace and comments, and returns the Lisp object denoted by the s-expression. • For instance, suppose c:/CLISP/name.txt has the following contents: (1 2 3) 456 "a string" ; this is a comment ((a b) (c d)) • In other words, it contains four s-expressions: a list of numbers, a number, a string, and a list of lists.

  6. READING FILE DATA CL-USER> (defparameter *s* (open "c:/CLISP/name.txt")) *S* CL-USER> (read *s*) (1 2 3) CL-USER> (read *s*) 456 CL-USER> (read *s*) "a string" CL-USER> (read *s*) ((A B) (C D)) CL-USER> (close *s*) T

  7. READING BINARY DATA • By default OPEN returns character streams, which translate the underlying bytes to characters according to a particular character-encoding scheme. • To read the raw bytes, we need to pass OPEN an :element-type argument of '(unsigned-byte 8). • We can pass the resulting stream to the function READ-BYTE, which will return an integer between 0 and 255 each time it's called. • READ-BYTE, like the character-reading functions, also accepts optional arguments to specify whether it should signal an error if called at the end of the file and what value to return if not. • The examples are in Chapter 24.

  8. BULK READS • READ-SEQUENCE works with both character and binary streams. • We pass it a sequence (typically a vector) and a stream, and it attempts to fill the sequence with data from the stream. • It returns the index of the first element of the sequence that wasn't filled or the length of the sequence if it was able to completely fill it. • We can also pass :start and :end keyword arguments to specify a subsequence that should be filled instead. • The sequence argument must be a type that can hold elements of the stream's element type. • Since most operating systems support some form of block I/O, READ-SEQUENCE is likely to be quite a bit more efficient than filling a sequence by repeatedly calling READ-BYTE or READ-CHAR. • The examples are in Chapter 23.

  9. FILE OUTPUT • To write data to a file, we need an output stream, which we obtain by calling OPEN with a :direction keyword argument of :output. (open "c:/CLISP/ww.txt" :direction :output :if-exists :supersede) • When opening a file for output, OPEN assumes the file shouldn't already exist and will signal an error if it does. • However, we can change that behavior with the :if-exists keyword argument. • Passing the value :supersede tells OPEN to replace the existing file. • Passing :append causes OPEN to open the existing file such that new data will be written at the end of the file, while :overwrite returns a stream that will overwrite existing data starting from the beginning of the file. • And passing NIL will cause OPEN to return NILinstead of a stream if the file already exists.

  10. FILE OUTPUT • Some functions for writing data: • WRITE-CHAR writes a single character to the stream. • WRITE-LINE writes a string followed by a newline, which will be output as the appropriate end-of-line character or characters for the platform. • WRITE-STRING writes a string without adding any end-of-line characters. • Two different functions can print just a newline: • TERPRI—short for "terminate print"—unconditionally prints a newline character. • FRESH-LINE prints a newline character unless the stream is at the beginning of a line.

  11. FILE OUTPUT • Several functions output Lisp data as s-expressions: • PRINT prints an s-expression preceded by an end-of-line and followed by a space. • PRIN1 prints just the s-expression. • PPRINT prints s-expressions like PRINT and PRIN1 but using the "pretty printer," which tries to print its output in an aesthetically pleasing way. • For example: (let ((stream (open c:/CLISP/www.txt" :direction :output :if-exists :overwrite))) (print "This is a test..." stream) (close stream))

  12. FILE OUTPUT • The variable *PRINT-READABLY* controls what happens if you try to print such an object with PRINT, PRIN1, or PPRINT. • When it's NIL, these functions will print the object in a special syntax that's guaranteed to cause READ to signal an error if it tries to read it; otherwise they will signal an error rather than print the object. • PRINC also prints Lisp objects, but in a way designed for human consumption. • For instance, PRINC prints strings without quotation marks.

  13. FILE OUTPUT • To write binary data to a file, we have to OPEN the file with the same :element-type argument as we did to read it: '(unsigned-byte 8). • We can then write individual bytes to the stream with WRITE-BYTE. • The bulk output function WRITE-SEQUENCE accepts both binary and character streams as long as all the elements of the sequence are of an appropriate type for the stream, either characters or bytes. • This function is likely to be quite a bit more efficient than writing the elements of the sequence one at a time.

  14. CLOSING FILES • It's important to close files when we are done with them, because file handles tend to be a scarce resource. • It might seem straightforward enough to just be sure every OPEN has a matching CLOSE. • For instance, we could always structure our file using code like this: (let ((stream (open "c:/CLISP/name.txt"))) ;; do stuff with stream (close stream)) • This approach suffers from two problems. • If you forget the CLOSE, the code will leak(洩漏) a file handle every time it runs. • There's no guarantee you'll get to the CLOSE.

  15. CLOSING FILES • Common Lisp provides a macro, WITH-OPEN-FILE, to encapsulate (壓縮) the pattern of opening a file. • This is the basic form: (with-open-file (stream-var open-argument*) body-form*) • The forms in body-forms are evaluated with stream-var bound to a file stream opened by a call to OPEN with open-arguments as its arguments. • WITH-OPEN-FILE then ensures the stream in stream-var is closed before the WITH-OPEN-FILE form returns. • For examples: (with-open-file (stream "c:/CLISP/name.txt") (format t "~a~%" (read-line stream))) (with-open-file (stream "c:/CLISP/ww.txt" :direction :output) (format stream "Some text."))

  16. READING FILES WITH WITH-OPEN-FILE • Suppose the file "name.txt" in the directory c:/CLISP contains these lines: "The North Slope" ((45 redwood) (12 oak) (43 maple)) 100 • We can read this data with the following program: (defun get-tree-data () (with-open-file (stream "c:/CLISP/name.txt") (let* ((tree-loc (read stream)) (tree-table (read stream)) (num-trees (read stream))) (format t "~&There are ~S trees on ~S." num-trees tree-loc) (format t "~&They are: ~S" tree-table))))

  17. READING FILES WITH WITH-OPEN-FILE > (get-tree-data) There are 100 trees on "The North Slope". They are: ((45 REDWOOD) (12 OAK) (43 MAPLE)) NIL

  18. WRITING FILES WITH WITH-OPEN-FILE • We can also use WITH-OPEN-FILE to open files for output by passing it the special keyword argument :DIRECTION :OUTPUT. (defun save-tree-data (tree-loc tree-table num-trees) (with-open-file (stream "c:/CLISP/www.txt" :direction :output) (format stream "~S~%" tree-loc) (format stream "~S~%" tree-table) (format stream "~S~%" num-trees))) > (save-tree-data "The West Ridge" '((45 redwood) (22 oak) (43 maple)) 110) NIL

More Related