However, not all objects can be printed in a form that READ
will understand. 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.
Another function, PRINC
, also prints Lisp objects, but in a way designed for human consumption. For instance, PRINC
prints strings without quotation marks. You can generate more elaborate text output with the incredibly flexible if somewhat arcane FORMAT
function. I'll discuss some of the more important details of FORMAT
, which essentially defines a mini-language for emitting formatted output, in Chapter 18.
To write binary data to a file, you have to OPEN
the file with the same :element-type
argument as you did to read it: '(unsigned-byte 8)
. You 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. As with READ-SEQUENCE
, this function is likely to be quite a bit more efficient than writing the elements of the sequence one at a time.
As anyone who has written code that deals with lots of files knows, it's important to close files when you're done with them, because file handles tend to be a scarce resource. If you open files and don't close them, you'll soon discover you can't open any more files.[155] It might seem straightforward enough to just be sure every OPEN
has a matching CLOSE
. For instance, you could always structure your file using code like this:
(let ((stream (open '/some/file/name.txt')))
;; do stuff with stream
(close stream))
However, this approach suffers from two problems. One is simply that it's error prone—if you forget the CLOSE
, the code will leak a file handle every time it runs. The other—and more significant—problem is that there's no guarantee you'll get to the CLOSE
. For instance, if the code prior to the CLOSE
contains a RETURN
or RETURN-FROM
, you could leave the LET
without closing the stream. Or, as you'll see in Chapter 19, if any of the code before the CLOSE
signals an error, control may jump out of the LET
to an error handler and never come back to close the stream.
Common Lisp provides a general solution to the problem of how to ensure that certain code always runs: the special operator UNWIND-PROTECT
, which I'll discuss in Chapter 20. However, because the pattern of opening a file, doing something with the resulting stream, and then closing the stream is so common, Common Lisp provides a macro, WITH-OPEN-FILE
, built on top of UNWIND-PROTECT
, to encapsulate this pattern. This is the basic form:
(with-open-file (
The forms in OPEN
with WITH-OPEN-FILE
then ensures the stream in WITH-OPEN- FILE
form returns. Thus, you can write this to read a line from a file:
(with-open-file (stream '/some/file/name.txt')
(format t '~a~%' (read-line stream)))
To create a new file, you can write something like this:
(with-open-file (stream '/some/file/name.txt' :direction :output)
(format stream 'Some text.'))
You'll probably use WITH-OPEN-FILE
for 90-99 percent of the file I/O you do—the only time you need to use raw OPEN
and CLOSE
calls is if you need to open a file in a function and keep the stream around after the function returns. In that case, you must take care to eventually close the stream yourself, or you'll leak file descriptors and may eventually end up unable to open any more files.
So far you've used strings to represent filenames. However, using strings as filenames ties your code to a particular operating system and file system. Likewise, if you programmatically construct names according to the rules of a particular naming scheme (separating directories with /, say), you also tie your code to a particular file system.
To avoid this kind of nonportability, Common Lisp provides another representation of filenames: pathname objects. Pathnames represent filenames in a structured way that makes them easy to manipulate without tying them to a particular filename syntax. And the burden of translating back and forth between strings in the local syntax—called
Unfortunately, as with many abstractions designed to hide the details of fundamentally different underlying systems, the pathname abstraction introduces its own complications. When pathnames were designed, the set of file systems in general use was quite a bit more variegated than those in common use today. Consequently, some nooks and crannies of the pathname abstraction make little sense if all you're concerned about is representing Unix or Windows filenames. However, once you understand which parts of the pathname abstraction you can ignore as artifacts of pathnames' evolutionary history, they do provide a convenient way to manipulate filenames.[156]
Most places a filename is called for, you can use either a namestring or a pathname. Which to use depends mostly on where the name originated. Filenames provided by the user—for example, as arguments or as values in configuration files—will typically be namestrings, since the user knows what operating system they're running on and shouldn't be expected to care about the details of how Lisp represents filenames. But programmatically generated filenames will be pathnames because you can create them portably. A stream returned by OPEN
also represents a filename, namely, the filename that was originally used to open the stream. Together these three types are collectively referred to as
How We Got Here |
The historical diversity of file systems in existence during the 70s and 80s can be easy to forget. Kent Pitman, one of the principal technical editors of the Common Lisp standard, described the situation once in comp.lang.lisp (Message-ID: |