Function Definitions

 15  -  Files

open ..... Opens a file and returns a file descriptor (a pointer to the memory buffer associated with the file) which is used later to identify the file buffer to input and output functions

(setq fw (open "file1.txt" "w"))  opens the file named FILE1.TXT for writing and assigns a file descriptor to variable fw. The "w" must be lower case. When opening a file for writing, a new file is created. If an old file by the same name exists, it is overwritten.

(setq fa (open "file2.txt" "a"))  opens the file named FILE2.TXT to be appended assuming the file already exists. If it does not already exist, it is created. The "a" must be lower case.

(setq fr (open "file3.txt" "r"))  opens the file named FILE3.TXT for reading and assigns a file descriptor to variable fr. The "r" must be lower case. When opening a file for reading, the file must already exist or the open function returns nil.

(setq f7 (open "a:\\stock\\file7.txt" "w"))  opens file FILE7.TXT in folder A:\STOCK for writing.  (A single forward slash can be used in place of the double backslash.  Thus, the previous file name is equivalent to "a:/stock/file7.txt")


close ..... Closes a file

(close f4)  closes the file identified by file descriptor f4 and returns nil.

The argument must be a variable holding the descriptor of a file opened with the open function.  If the file was opened for writing, any contents in the file buffer not yet sent to disk are sent.


read-line ..... Reads a string from the keyboard buffer or the next line from a file and returns the line as a string.  This string does not include the carriage return or line feed from the end of the line in the file.

(read-line f9)  reads one line from the file identified by the file descriptor f9 (which was previously opened for reading).  If there are no more lines to be read, read-line returns nil

(read-line) reads a line from the keyboard buffer. If there are no lines in the buffer, it waits.

To reset the file pointer to the first string, close then re-open the file for reading. 


write-line ..... Writes one line to screen or to file.  Places a carriage return (ASCII 13) and line feed (ASCII 10) at the end of each line.

(write-line "Abraham Lincoln")  writes Abraham Lincoln on the screen and returns "Abraham Lincoln"

(write-line "Abraham Lincoln" f8)  writes Abraham Lincoln to the file identified by file descriptor f8 (which was previously opened for writing) and returns "Abraham Lincoln"


read-char ..... Reads one character from the keyboard buffer or the next character from a file and returns the character's ASCII code number.  When read-char encounters the end of a line (newline), it returns ASCII code 10. When it encounters the end of the file, it returns nil. To reset the file pointer to the first character, close then re-open the file for reading.

(read-char f7)  reads one character from the file identified by the file descriptor f7 (which was previously opened for reading)

(read-char)  reads one character from the keyboard buffer. If there are no characters in the buffer, it waits.


write-char ..... Writes one character to screen or to file

(write-char 65)  writes "A" to screen and returns 65
(write-char 65 f8)  writes "A" to the file identified by file descriptor f8 (which was previously opened for writing) and returns 65


findfile ..... Searches for a specified file

If no path is supplied, AutoLISP will look in the AutoCAD search path for the file. If a path is supplied, only that path is searched for the file.  The AutoCAD search path includes the current folder, the folder holding the current drawing file, plus the folders listed in AutoCAD under "Preferences," under the "Files" tab, under "Support file search path."

If the specified file is found, a string containing the file's full name (including path) is returned. Otherwise nil is returned.  For example, if the file Pen.txt exists only in the \Work folder (which is the current folder and/or the folder holding the current drawing), then

(findfile "Pen.txt")  returns "C:\\Work\\Pen.txt"
(findfile "\\Alpha\\Pen.txt")  returns nil

Remember, in a string describing a file path, the double backslash equals a single forward slash.


getfiled ..... Displays a standard file dialog box and allows user to select the name of an existing file or the name of a new file to create.  Returns the name of the file.

(getfiled "Select block to insert" "c:\\blocklib\\standard.dwg" "dwg" 0)  displays a file dialog box with the title "Select block to insert" at the top, with the c:\blocklib folder opened and "standard.dwg" shown in the file name box as the the default file, and with only "dwg" files listed.  The fourth argument (0) allows user to select an existing file rather than create a file.

Other settings for the fourth argument:
  1 = Create a new file
  2 = Disable the "Type it" button
  4 = Let user enter any file extension even if different than that indicated in the third argument
  8 = Return only file name rather than full path to file (unless other files by same name exist in other folders
These can be combined in bitwise fashion.  For example, 5 = (1 + 4) would create a new file and let the user enter any extension.


HOME


Copyright © 1988, 1998 Ronald W. Leigh