PRACTICAL COMMON LISP Peter Seibel 1.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Formatted I/O The read and print statements are very limited –they default to using the standard input and output devices –if we want to redirect I/O,
Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.
Chapter 10.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
PRACTICAL COMMON LISP Peter Seibel 1.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to C Programming
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Chapter 2 How to Compile and Execute a Simple Program.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
PRACTICAL COMMON LISP Peter Seibel 1.
Lexical Analysis Hira Waseem Lecture
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
(Stream Editor) By: Ross Mills.  Sed is an acronym for stream editor  Instead of altering the original file, sed is used to scan the input file line.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Week 1 Algorithmization and Programming Languages.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Input Streams “A program designed for inputs from people is usually stressed beyond the breaking point by computer-generated inputs.” Dennis Ritchie, Bell.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
University of Central Florida COP 3330 Object Oriented Programming
Files and Streams Lect3 CT1411.
“The fastest I/O is no I/O.” Nils-Peter Nelson, Bell Labs
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
Topics Introduction to File Input and Output
Peter Seibel Practical Common Lisp Peter Seibel
Topics Introduction to File Input and Output
Introduction to Computer Science
Presentation transcript:

PRACTICAL COMMON LISP Peter Seibel 1

CHAPTER 14 FILES AND FILE I/O 2

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)) 3

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))) 4

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. 5

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 6

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. 7

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. 8

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 NIL instead of a stream if the file already exists. 9

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. 10

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/ :direction :output :if- exists :overwrite))) (print "This is a test..." stream) (close stream)) 11

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. 12

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. 13

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. 14

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.")) 15

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)))) 16

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 17

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/ :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 18