Skip to content

Instantly share code, notes, and snippets.

@juan-reynoso
Created October 28, 2021 22:51
Show Gist options
  • Save juan-reynoso/494547d0490343d8fdbbf91c6ed9cbee to your computer and use it in GitHub Desktop.
Save juan-reynoso/494547d0490343d8fdbbf91c6ed9cbee to your computer and use it in GitHub Desktop.
A file handling example
(defparameter *line* nil "This is a global variable")
;;; an example
(with-open-file (file "/tmp/myfile.txt" :direction :input)
(loop
;; gets a line from the file
(setf *line* (read-line file nil))
(if *line*
;; print the lines stored in the text file
(format t "~% ~a ~%" *line*)
;; End Of File
(return t))))
;;; :direction :input => meaning you want to read from the file
;;; :direction :output (meaning you want to write to the file)
;;; :if-exists :supersede => meaning that the new file will replace the old one)
;;; :if-exists :append => opens the file to append data to it
(defun load-data (filename)
;; :direction :input => meaning you want to read from the file
(with-open-file (file filename :direction :input)
(loop
;; gets a line from the file
(setf *line* (read-line file nil))
(if *line*
;; print the lines stored in the text file
(format t "~a ~%" *line*)
;; Enf Of File
(return t)))))
(defun store-data (filename data)
;; :direction :output (meaning you want to write to the file)
;; :if-exists :append => opens the file to append data to it
(with-open-file (file filename :direction :output :if-exists :append)
;; writes a line in the file
(write-line data file))
(format t "The file was written"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment