The AutoLisp Intermediate Tutorial


Read/Write & File Functions

Functions  - open/close  read-line   write-line  Example Programs


open/close - Open a file and close a file.  That simple.

                Syntax : (open "filename" mode)  (close fileVarName)

                      Where filename is any valid file or resource name.

                       Where mode is either "r" , "w" ,  or  "a".

                           "r"   -  Read mode.  Reads a file with either read-char or read-line.

                           "w" - Write mode.  Writes to a file with either write-char, write-line, princ, or print.

                           "a"   - Append mode.  Appends to an existing file using write-char, write-line, princ, or print.

                                    Note: If the existing file is not found in append mode, autolisp will create a new empty file.

               Open Returns a file descriptor.

                      Note: You must use (setq varNam(open "file" mode)) to enable closing of the file.

                      Note: Writing the file does not occur until you use the close statement.
.

                      Example:

          (if (setq f(open "c:/acad/myfile.txt" "r"))

            (progn

              (while (setq txtLine(read-line f))

                 (princ txtLine)

              )

              (close f)

            )

            (princ "\n Error - File was not opened.")

          )

 

read-line - This function reads a line of text from an open file.

                  Syntax   :  (read-line fileVar)

                       Where fileVar is a valid variable name that represents an open file descriptor.

               Returns a text string.

                Assuming (setq f(open "text.txt" "r")) returned sucessfully.

                Assuming (setq g(open "text8.txt" "r")) failed.

             (read-line f)                    ;returns string from file

                (read-line g)                    ;returns Error - Bad Argument Type

 

write-line - This function writes a line of text to an open file.

                  Syntax   :  (write-line fileVar)

                       Where fileVar is a valid variable name that represents an open file descriptor.

               Returns a text string.

                Assuming (setq f(open "text.txt" "r")) returned successfully.

                Assuming (setq g(open "text8.txt" "r")) failed.

             (write-line "Hello World" f)     ;writes text to file and returns string "Hello World"

                (write-line "Hello World" g)     ;returns Error - Bad Argument Type

 

End of Read/Write and File Functions


Final note:          

  I've stated over and over that the OPEN statement opens a file.  This can also be any resource available to you.   Instead of opening "C:\ACAD\MyTextFile.txt" you can just as easily open "LPT1" or "\\myserver\myprinter" and write the file to a printer exactly like you write to a file.  Cool!  Printing from AutoLisp! 

  One thing to keep in mind.  The file does not get written to disk or sent to the printer until after the CLOSE statement. [ This will help you when debugging.]   If you cannot get a printer to print,  write the data into a text file instead of the printer.  Then open the file with notepad and see if it is what you expect.  If the data looks good in notepad then you've probably got the printer name wrong, a printer problem, or a network access problem.


Example Program 1:

Let's write a program that prints a text file on the command line.

(defun C:pTxt()                                                         ;define a program name

     (setq fname(getstring "\n Enter a valid file name: "))  ;get file name cheaply

      (if(setq f(open fname" "r"))                    ;open the file in read mode

       (while (setq txtLine(read-line f))   ;while lines of text exist in file

        (princ txtLine)                                     ;print the text string to the command line

       )                                      ;close the while statement

       (princ "\n Error - Could not find file") ;print a message on the else statement

   )                                                                     ;close the if statement

          ; note: the if statement can be a " if then " statement or a " if then else" statement

)                                          ;close the program

 


AutoLisp Intermediate Tutorial

AutoLisp Tutorial Home