Wednesday
AutoLISP, ProgrammingManaging basic file operations with AutoLISP
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
Viewed 6,155 times so far... This week: 35 Today: 9 Latest: 4 July 2009, 23:52Post Tags: AutoLISP, Programming
Related Posts
Popular
- Hip tip: Increasing view resolution with VIEWRES system variable
07/03/2009 04:39 am
7 Comments - Sirince – IZMIR
07/02/2009 11:02 am
1 Comment - Visual LISP: Programming user interfaces with OpenDCL
07/01/2009 10:15 pm
10 Comments - 3D Modeling 7: EXTRUDE
06/30/2009 02:14 pm
10 Comments - Ask DailyAutoCAD: Which Notebook to Buy?
06/30/2009 12:17 pm
1 Comment
Featured Articles
- AutoCAD 2009 Update 3 Released
- Speed Up As AutoCAD 2009 Ribbon Menu Helps You!
- MEP Analysis Extension for AutoCAD MEP extended to March 31, 2009
- Recap of AutoCAD-related technologies on Autodesk Labs
- Technology Preview of MEP Analysis Extension extended to 12/31/08
- Autodesk Labs: Forward and Backward utility for AutoCAD
|

Oct 24, 2007
Reply
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.
Oct 24, 2007
Reply
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
Jul 13, 2008
Reply
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
Aug 25, 2008
Reply
I want to read the commands from xml file. Pls help me i am very new to this Autolisp.
Mar 28, 2009
Reply
I want to ask if you have an auto lisp file appears every time you open your Auto-CAD program as another file beside your auto cad file named acad and the file type AutoLISP Application source… please if you had the same problem and you fix it tell me how to do it.