Open In App

File Handling in LISP

Improve
Improve
Like Article
Like
Save
Share
Report

LISP is an acronym for list processing, it is one the oldest programming language currently being used in the field of artificial intelligence. It performs its computations on symbolic expressions which makes it too reliable for AI.

File handling is a method by which we can access files and store data in them after executing input and output operations for future references since the data is not lost. 

In this article, we will learn the very basics of file manipulation with LISP.

1. Opening a file in LISP:

  The most basic task is to read the content of a file. In LISP we have the OPEN function with which we can open an existing file or create a new file.

  Syntax:

   open filename &key :direction :element-type :if-exists :if-does-not-exist :external-format

 In the syntax given above, we have multiple Keyword arguments. We should note that Keyword arguments are helpful in defining the type of stream and different error handling ways.

Parameters:

  •  Direction: Defines what stream should handle  input (default) , :output, :io (both input and output), :probe (checks file existence)
  •  Element-type:  Type of element in the stream.
  •  If-exists: It is used for checking conditions. Format it can take.
:error - for error signaling.
:rename - for renaming an existing file.
nil - indicates failure.
:rename-and-delete -first rename an existing file then delete it.
:append - Appending to an existing file.
  •  if-does-not-exist: Triggered when the file to be opened does not exist. Format it can take:
: error - for error signaling.
: create - creates an empty file with the name specified for further use.
nil - indicates failure.
  •   external-format: Specifies the scheme for character representation in files.

 Syntax:

(open “helloworld.file” :direction :output :if-exists :append :if-does-not-exist :create)

After execution of the above command, a file is opened or a new file is created if it does not exist.

2. Reading from and Writing to a file in LISP:

 With the help of the WITH-OPEN-FILE macro, a file can be opened for both reading and writing with the automatic closing of the file.

  Syntax:

 with-open-file (stream filename {options}*) {declaration}* {form}*

  •   filename: refers to the name of the file to be opened.
  •   options: keyword arguments.

Example 1: 

Lisp




(write-line "Hello World")
; Writing to a file
 
(with-open-file (file "helloworld.txt" :direction :output
:if-exists :supersede
:if-does-not-exist :create)
 
; Here :supersede supersedes (replaces) the preexisting file and
; the :direction :output ensures that the file is
; opened for the writing purpose.
 
(dolist (line '("Banana" "Guava" "Apple" "Cherry" "Mango"
                         "Strawberry" "Pineapple" "Avocado" 
))
 
(write-line line file)))
 
; Since with open file is used so file is closed automatically
; Reading from a file
 
(let ((in (open "helloworld.txt" :if-does-not-exist nil)))
 
; :if does not exist set to null means there's
; no file to read from so indicate failure
      
(when in
 
(loop for line = (read-line in nil)
 
while line do (format t "~a~%" line))
 
(close in)
)
)
;  stream" in"  is closed with close command at last


  Output: 

 

  3. Closing a file in LISP:

Closing a file in LISP is performed using CLOSE which closes a stream, by closing a stream it is ensured that the stream won’t be used for any input or output operation. All the association between stream and file ends with the CLOSE command.

Note: Closing a file with CLOSE is not required with the WITH_OPEN_FILE macro as it is closed automatically.

 

Example 2 :

Lisp




;Writing to a file
 
(let ((file (open "helloworld.txt" :direction :output
:if-exists :supersede
:if-does-not-exist :create)))
 
(dolist (line '("computer network" "DSA" "Data Mining"
 
"C++" "C#" "MATLAB"))
 
(write-line line file))
(close file))
 
;Stream "file" closed with close command at last
; Reading from a file
 
(let ((in (open "helloworld.txt" :if-does-not-exist nil)))
 
(when in
 
(loop for line = (read-line in nil)
 
while line do (format t "~a~%" line))
 
(close in)
)
)
 
; Stream "in" closed with close command at last


 Output:

 



Last Updated : 16 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads