The following subfunctions might be handy for learning (regarding file-handling) :
- ; First create a ""My_F2L2F_Example.txt" file and place it within trusted acad path _$ (List->File "My_F2L2F_Example.txt" '("Hello," "It's me" "the Grrr guy" "from CADtutor.") )"C:\\Users\\Grrr\\desktop\\trusted acad path\\My_F2L2F_Example.txt"_$ (File->List "C:\\Users\\Grrr\\desktop\\trusted acad path\\My_F2L2F_Example.txt")("Hello," "It's me" "the Grrr guy" "from CADtutor.")_$ (File->List (List->File "My_F2L2F_Example.txt" '("This" "is" "entirely new" "content" "that has" "been writen" "and returned.") ))("This" "is" "entirely new" "content" "that has" "been writen" "and returned."); (List->File "My_F2L2F_Example.txt" '("Hello," "It's me" "the Grrr guy" "from CADtutor.") ); find_what - can be a filename with extension or a full filepath with filename and extension, i.e.: "Example.txt" or "C:\\Users\\Documents\\Example.txt"; L - list of strings(defun List->File ( find_what L / find desc ) (if (and ; wrap all evaluations, using (and) so the program will stop on the first evaluation that returned nil L (listp L) ; make sure that a list is provided, and its a list, since (listp nil) -> T (vl-every '(lambda (x) (eq 'STR (type x))) L) ; make sure that every item inside the list is a string (setq find (findfile find_what)) ; find the fine, should return the file's path as a string [sTR] (setq desc (open find "w")) ; obtain file's descriptor (open for writing), using the file's path (mapcar '(lambda (x) (write-line x desc)) L) ; write each item of the list as a new line, inside the file (and desc (not (close desc))) ; close the file's descriptor, (close desc) -> nil ; so we use: (not (close desc)) -> T ); and find ; if everything is ok, return the file path we have found, else nil ); if ); defun List->File; (File->List "My_F2L2F_Example.txt"); find_what - can be a filename with extension or a full filepath with filename and extension, i.e.: "Example.txt" or "C:\\Users\\Documents\\Example.txt"(defun File->List ( find_what / find desc row L ) (and ; wrap all evaluations, using (and) so the program will stop on the first evaluation that returned nil (setq find (findfile find_what)) ; find the fine, should return the file's path as a string [sTR] (setq desc (open find "r")) ; obtain file's descriptor (open for reading), using the file's path (progn ; wrap the following (while) loop inside a (progn), cause it would return nil (while (setq row (read-line desc)) ; using a loop obtain the 'nth' row, until an end-of-line marker is encountered (setq L (cons row L)) ; collect every row in a list ); while (and desc (not (close desc))) ; close the file's descriptor, (close desc) -> nil ; so we use: (not (close desc)) -> T ; this also is used to return true for the (progn) expression ); progn ); and (reverse L) ; reverse the list with collected rows, to return properly, (reverse nil) doesn't error out); defun File->List
I've wrote them as an example to samifox's questions, although would avoid the usage of (findfile) and rather expect a full path with filename and extension as an argument - because there are also functions like (getfiled) (vl-filename-mktemp)...