Managing basic file operations with AutoLISP
Print This Post
![]() |
Author | Orhan Toker |
| Profession | Architect M.Sc. | |
| Autodesk Authorized Consultant | ||
| Database Specialist | ||
Dear Daily AutoCAD readers,
I haven’t been writing about AutoLISP for a quite long time. The reason is that I was very busy with work and another reason is that whenever I write an article about AutoLISP, Muharrem writes an article about VisualLISP and exceeds me.
In this article, I would like to explain about the fundamental file operations that every user should learn who would like to advance in programming languages. After reading this article, you will be able to make all fundamental operations on files like opening-closing files and control the contents of any file.
Using (open filename mod) function
(open) function is used for opening files in AutoLISP. The filename is the fullname of the file including filename and directory. The most important issue at this stage is that directory separators are not reverse divide symbol “\” in Windows™ operating system but the normal divide symbol “/”.
Mod argument is used to determine in which mode the file will be opened.
“r” read-only mode
“w” write mode
“a” adding mode
Let’s take a look at how we are going to use them in one example:

Code.1
If we examine the code line by line, you will see that;
In 8th line, we are opening “BOOT.INI” that exists in your c: root directory in all of our computers in read-only mode. You might have noticed that the file is not being opened directly. Instead, we are opening the file by assigning file pointer value to f variable. This pointer value will be used throughout the function.
In 10th line, (read-line f) function reads the file line by line. this function uses the file pointer and reads the file until its end when it is assigned to a variable. Every time we use the (read-line) variable by using the same file pointer cursor goes one line below. (while) loop will continue until the (read-line f) is returned as ‘nil. By this way, we will have read the complete BOOT.INI file from beginning to end.
By this way, we finished opening files by using (open filename mod) function and reading from the file line by line by using (read-line file-pointer). And now, similarly but in the opposite way, let’s try creating a file and writing something inside it.

Code.2
As it can be clearly seen in code. 2, this time (open) function created the “try.txt” file in “w” mode and we wrote two lines inside it by using (write-line) function. Do not forget that, at the end of each code operation where you open the file, you should put (close file-pointer).
We will continue with Muharrem with other articles about file operations.
Technorati Tags: AutoLISP, file operations
Read in other languages: TR
Similar Posts
Rate this article:


October 24th, 2007 12:06
I need to be able to import a bitmap file into a drawing and locate and scale it using LISP. Any help would be appreciated. Thanks.
October 24th, 2007 14:42
Respected Sir,
I search more in places a lisp program for obtaining the dimension value, Upper tolerance value and Lower tolerance value in seperate order. If you give any tips for generate the lisp program for abow mentioned resion, it will more helpfull for me.
Thanking You.
ASHOK.S
November 15th, 2007 10:21
[...] read my Managing basic file operations in AutoLISP article before start programming. Let’s start with studying the txt file. [...]
July 13th, 2008 11:52
I am trying to import csv file format instead of txt file but i am not able to separate the values i have a 5 column csv data file so i use car cdr cadr etc functions to separate the values but i am not able to do it because i think the read mode does not convert the data obtained to a list but rather it is kept as a text separated by comma. how can i convert it to a list so that i can separate the values
Thanks
August 25th, 2008 05:17
I want to read the commands from xml file. Pls help me i am very new to this Autolisp.