Lisp - Writing Data To Streams



Understanding how to write data to streams is equally important as reading from streams. In this chapter, we'll explore various methods to write data to streams in various ways.

Core Output Functions

print() function

  • print() is a primary function to write data or LISP objects to output stream followed by a newline.

  • print() function is very useful in displaying values in readable format.

  • By default, print() method writes to *standard-output* connected to display. We can specify other streams as well using optional stream parameter.

princ() function

  • princ() writes a LISP object without adding a new line.

  • This function is useful when we need output without new line.

write() function

  • write() function is used to write to output stream when we need to read back by read() function.

  • This function preserves the structure of the LISP object.

Other write functions

  • write-char() function writes a single character to the output stream.

  • write-line() function writes a string to the output stream followed by a new line.

  • write-string() function writes a string to the output stream.

  • write-byte() function writes a single byte to the output stream.

format() function

  • format() function is used to write formatted output to the stream.

  • It allows us to control the layout of the output.

  • A format string is passed to specify the format in which values are to be displayed.

Key Considerations

Standard Output

A special variable *standard-output* represents the default output stream, generally connected to the display. print functions use *standard-input* by default but we can provide other stream as input as well.

File Output

We can open a file in output mode using open function with flag :direction as :output. A stream should be closed using close function once write operation is completed.

String Output

We can write data to strings in form of stream as well. Using macro with-output-to-string, we can achieve the same.

Buffering

While writing to output streams, the data may be buffered which means data is not written immidiately to the output source or underlying device. We can use force-output function to flush the buffer and to ensure that all data is written.

Error Handling

During I/O operations, error can popup in any form like invalid syntax, or error during I/O etc. Error should be handled properly in streams operation.

Example - Writing a File

In following example, we're writing a file test.txt in current folder.

main.lisp

; open output.txt in write mode
(let ((output-stream (open "output.txt" :direction :output :if-exists :supersede)))
  (when output-stream ; if output stream is available
    (format output-stream "The value of pi is ~5,3f~%" pi) ; write to file
    (close output-stream))) ; close the stream

Output

When you execute the code, it returns the following result −

T

Following is the content of the output.txt after running this program.

The value of pi is 3.142

Explanation

  • open function is used to open output.txt in write mode.

  • format function is used to write a line to a file.

  • close function is used to close the file after writing to it.

Advertisements