Tutorial by Examples

The macro WITH-INPUT-FROM-STRING can be used to make a stream from a string. (with-input-from-string (str "Foobar") (loop for i from 0 for char = (read-char str nil nil) while char do (format t "~d: ~a~%" i char))) ; 0: F ; 1: o ; 2: o ; 3: b ;...
The macro WITH-OUTPUT-TO-STRING can be used to create a string output stream, and return the resulting string at the end. (with-output-to-string (str) (write-line "Foobar!" str) (write-string "Barfoo!" str)) ;=> "Foobar! ; Barfoo!" The same can be done ...
Gray streams are a non-standard extension that allows user defined streams. It provides classes and methods that the user can extend. You should check your implementations manual to see if it provides Gray streams. For a simple example, a character input stream that returns random characters could ...
A file can be opened for reading as a stream using WITH-OPEN-FILE macro. (with-open-file (file #P"test.file") (loop for i from 0 for line = (read-line file nil nil) while line do (format t "~d: ~a~%" i line))) ; 0: Foobar ; 1: Barfoo ; 2: Quuxbar...
A file can be opened for writing as a stream using WITH-OPEN-FILE macro. (with-open-file (file #P"test.file" :direction :output :if-exists :append :if-does-not-exist :create) (dolist (line '("Foobar" &q...
Copy byte-per-byte of a file The following function copies a file into another by performing an exact byte-per-byte copy, ignoring the kind of content (which can be either lines of characters in some encoding or binary data): (defun byte-copy (infile outfile) (with-open-file (instream infile :d...
The following function reads an entire file into a new string and returns it: (defun read-file (infile) (with-open-file (instream infile :direction :input :if-does-not-exist nil) (when instream (let ((string (make-string (file-length instream)))) (read-sequence string instr...

Page 1 of 1