Created
October 28, 2021 22:51
-
-
Save juan-reynoso/494547d0490343d8fdbbf91c6ed9cbee to your computer and use it in GitHub Desktop.
A file handling example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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