I/O
Format
The most common way to print data to the screen is using the format
function. This is like C’s printf
, but embedding a whole language for printing.
This chapter of Practical Common Lisp contains a lot of useful format
directives.
File I/O
You can use with-open-file
to safely handle files.
For instance, here’s how we open a file data.txt
in your home directory for writing:
(with-open-file(stream(merge-pathnames#p"data.txt"(user-homedir-pathname)):direction:output;; Write to disk:if-exists:supersede;; Overwrite the file:if-does-not-exist:create)(dotimes(i100);; Write random numbers to the file(formatstream"~3,3f~%"(random100))))
You can read the file into a string using uiop:read-file-string
:
CL-USER>(uiop:read-file-string(merge-pathnames#p"data.txt"(user-homedir-pathname)))"44.000 95.000 5.000 97.000 ... 15.000"