1 / 13

Input Streams

Input Streams. “A program designed for inputs from people is usually stressed beyond the breaking point by computer-generated inputs.” Dennis Ritchie, Bell Labs. Reading. Primary reading function is read Reads one S-expression from the currently selected input device and returns it as a value

wardah
Download Presentation

Input Streams

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. Input Streams “A program designed for inputs from people is usually stressed beyond the breaking point by computer-generated inputs.” Dennis Ritchie, Bell Labs

  2. Reading • Primary reading function is read • Reads one S-expression from the currently selected input device and returns it as a value • Only uses first expression

  3. Read Example (1) >(defun my-square () (format t "Please type a number: ") (let ((x (read))) (format t "The number ~A squared is ~A.~%" x (* x x)))) MY-SQUARE >(my-square) Please type a number:2 The number 2 squared is 4. NIL

  4. Read Example (2) >(defun wage-calc () (format t "Please enter your hourly wage: ") (let ((wage (read))) (format t "Please enter hours worked: ") (let ((hours (read))) (format t "Your pay is: $~$" (* wage hours))))) WAGE-CALC >(wage-calc) Please enter your hourly wage:3 Please enter hours worked:5 Your pay is: $15.00 NIL

  5. Other Read Functions • read-char reads any character, including newlines • Returns character read • read-line reads an entire line up to and including the newline character • Returns a string with the characters read, not including the newline

  6. Peek-char • You can look at the next character in a stream without advancing the stream (peek-char mode stream) • If mode is nil, then you always get the next character in the stream • If mode is t, then peek-char will actually advance the stream past white space to peek at the next character that’s not white space

  7. make-string-input-string converts a string into an input stream >(defvar str (make-string-input-stream "one two three")) STR >(read-char str) #\o >(read-char str) #\n >(read-char str) #\e >(read str) TWO >(peek-char nil str) #\Space >(read-char str) #\Space >(peek-char nil str) #\Space >(peek-char t str) #\t >(read str) THREE String Input Stream

  8. End of Stream • listen tests whether there is an available character in a stream • By default, all read functions signal an error if there is nothing left to read • Giving these read functions a second argument of nil makes them return NIL instead >(read (make-string-input-stream "") nil) NIL • Giving yet another argument allows you to specify what value to return when the end of the stream is hit >(read (make-string-input-stream "") nil 'EOS) EOS

  9. More Input Stream Functions • clear-input flushes the contents of a stream • open-stream-p tests if a stream is open • close turns off a stream

  10. File IO • Open streams to files using the open function (open stream-name :direction :input) (open stream-name :direction :output) • open returns a stream • stream-name is usually a quoted file path • probe-file checks to see if a file exists

  11. File IO Example >(probe-file "test.txt") NIL >(defvar str (open "test.txt" :direction :output)) STR >(probe-file "test.txt") #p"/v/filer4b/v20q001/schrum2/test.txt" >(format str "We can write anything!~%") NIL >(close str) T

  12. With-Open-File • Remembering to close open streams can be cumbersome, so there is a shortcut (with-open-file (sym filename) S-expr*) • Automatically closes the file when control leaves the expression • Can take a :direction of :input or :output • Can also specify what to do if file already exists, e.g. :if-exists :supersede

  13. With-Open-File Example (defun print-name-help (str name) (format str "My name is ~A" name)) (defun print-name (filename) (with-open-file (str filename :direction :output :if-exists :supersede) (format t "Enter your name: ") (let ((name (read))) (print-name-help str name))))

More Related