I want to read in the contents of a file into a list. Some of my attempts so far have been -
(defun get-file (filename)
(let ((x (open filename)))
(when x
(loop for line = (read-line x nil)
while line do (list line)))
(close x)))
(defun get-file (filename)
(let ((x (open filename :if-does-not-exist nil)) (contents (list nil)))
(when x
(loop for line = (read-line x nil)
while line do (cons contents line)))
(close x) contents))
(defun get-file (filename)
(let ((x (open filename :if-does-not-exist nil)) (contents nil))
(when x
(loop for line = (read-line x nil)
while line do (append contents line)))
(close x) contents))
None of these worked. Can anyone tell me a way? Or even better - how to put all of the contents into an array?
I'll add libraries in.
edit even easier, with
uiop
, which is included in ASDF:https://github.com/fare/asdf/blob/master/uiop/stream.lisp#L445
also has
With Alexandria's
read-file-into-string
and split-sequence:With str:
Where are the problems?
DO will just execute something for side effects.
COLLECT will collect the result and the LOOP then will return a list of collected values upon exit.
As already mentioned, use WITH-OPEN-FILE instead of OPEN/CLOSE. WITH-OPEN-FILE will close the file upon leaving the dynamic scope. Not just from a normal exit, but also upon error conditions using UNWIND-PROTECT.
If you want to read the contents of a file, you can use READ-SEQUENCE. With the usual problems. For example when you read an ASCII file as text into a string, the string may be shorter than the file. For example Common Lisp will represent internally CRLF with a single character, on platforms where CRLF is newline. Another example: in Unicode supporting implementations the UTF-8 code in the file may be replaced with a single character.
How about