(open filename mode)


Open that file.


This function makes a file's filename accessible for input and output functions. The filename argument represents the path and filename of the file to open.

The open function returns a file descriptor (an 8-digit hexadecimal number) for the opened file, which should be stored as a variable using the setq function. Once you have the file descriptor, you must refer to the file using the file descriptor-not the filename!

The mode argument has three options, which must be in lowercase:

Mode Meaning
"r" Open the file as read-only; returns nil if the file does not exist.
"w" Open the file for writing; overwrites existing data; creates a new file if non-existent.
"a" Open the file for writing; append to existing data; creates a new file if non-existent.

Examples

Code Returns
(setq n (open "existing.txt" "w")) <File: #2190dec2>
(setq n (open "noexist.lsp" "r")) nil
(setq n (open "\\sample\\exiting.txt" "a") <File: #2190c2c3>

NOTES

  • In append mode, new data is added to the end of the existing file.
  • LISP only reads ASCII files (no binary files) using sequential access (no random access).
  • To separate subdirectory names, you use either two backslashes ( \\ ) or a single forward slash ( / ).

Tell me about...

(close file-descriptor)

(read-line [file-descriptor])

(write-line string [file-descriptor])

LISP Compatibility

Programming Overview of LISP (LISt Processing) Language