Our Friends: Leaflet Printing , Presented by: Talia Cad Software

Sign In

Subscribe

Subscribe to Daily Autocad.


Oct 29
Wednesday
AutoLISP, Programming
Managing 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:

Fig.1
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.

Fgi.2
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: ,

Read in other languages: TR

Viewed 6,155 times so far... This week: 35 Today: 9 Latest: 4 July 2009, 23:52

Post Tags: ,

6 Responses to “ Managing basic file operations with AutoLISP ”
  1. 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.

  2. 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

  3. 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

  4. I want to read the commands from xml file. Pls help me i am very new to this Autolisp.

  5. 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.


Post a Comment



All content and source © 2008 Daily Autocad | News Plus wordpress theme brought to you by Zidalgo.