Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

12. Working with Streams > Working with Files

Working with Files

In addition to using streams to write to and read from the REPL, we can also use streams to write to and read from files.

You can create a file stream in Common Lisp in several ways. The best way is to use the with-open-file command. As you’ll see shortly, this command contains special bug-prevention features that make it safer to use than other available file commands. The following example uses with-open-file to write the string "my data" to a file named data.txt:

> (with-open-file (my-stream "data.txt" :direction :output)
      (print "my data" my-stream))

In this example, the with-open-file command binds the output stream to the name my-stream . This causes a file output stream to be created with the name my-stream. This stream will be available within the body of the with-open-file command (until the final closing bracket ), and any data we send to this stream will end up in the file named data.txt on the disk. The print command references my-stream as the destination for its output . Therefore, after running this example, you should find a new file named data.txt in the folder from which you launched CLISP. This file has the text "my data" as its content.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint