Satish Lele
satish.lele@gmail.com


File Handling Functions
findfile: Searches the AutoCAD library path for the specified file. (findfile filename). findfile function makes no assumption about the file type or extension of filename. If filename does not specify a drive/directory prefix, findfile searches the AutoCAD library path. If a drive/directory prefix is supplied, findfile looks only in that directory. findfile function always returns a fully qualified drive/directory/file name or nil if the specified file is not found.
If the current directory is /acad and it contains the file abc.lsp. (findfile "abc.lsp") returns "/acad/abc.lsp". If you are editing a drawing in the /acad/drawings directory, the ACAD environment variable is set to /acad/support, and the file xyz.txt exists only in the /acad/support directory. (findfile "xyz.txt") returns "/acad/support/xyz.txt"
If the file nosuch is not present in any of the directories on the library search path. (findfile "nosuch") returns nil. The fully qualified name returned by findfile is suitable for use with the open function. (open filename mode): Opens a file for access by the AutoLISP I/O functions.

getfiled: Prompts the user for a file name with the standard AutoCAD file dialog box, and returns that file name (getfiled title default ext flags). The title argument specifies the dialog box label, default specifies a default file name to use (which can be a null string [""]), and ext is the default file name extension. If ext is passed as a null string [""], it defaults to * (all file types). If the file type dwg is included in the ext argument, the getfiled function displays an image preview in the dialog. The flags argument is an integer value (a bit-coded field) that controls the behavior of the dialog box. To set more than one condition at a time, add the values together to create a flags value between 0 and 15.

open: Opens a file for access by the AutoLISP I/O functions. (open filename mode). The filename argument is a string that specifies the name and extension of the file to be opened. The mode argument is the read/write flag. It must be a string containing a single lowercase letter. The following table describes the valid mode letters. Mode options for the open function.
Open mode and Description
"r" Open for reading. If filename does not exist, open returns nil.
"w" Open for writing. If filename does not exist, a new file is created and opened. If filename already exists, its existing data is overwritten.
"a" Open for appending. If filename does not exist, a new file is created and opened. If filename already exists, it is opened and the pointer is positioned at the end of the existing data, so new data you write to the file is appended to the existing data.

read-line: Reads a string from the keyboard or from an open file. (read-line [file-desc]). If read-line encounters the end of the file, it returns nil; otherwise it returns the string that it reads. For example, assuming that f is a valid open file pointer, (read-line f) returns the next input line from the file, or returns nil if the end-of-file has been reached.

write-line: Writes a string to the screen or to an open file. (write-line string [file-desc]). It returns string quoted in the normal manner but omits the quotes when writing to the file. For example, assuming that f is a valid open file descriptor, (write-line "Test" f) writes Test and returns "Test".

princ / print: Prints an expression to the command line or writes an expression to an open file. (princ [expr [file-desc]]). The expr argument does not need to be a string. If file-desc is present and is a file descriptor for a file opened for writing, expr is written to the file exactly as it appears on the screen. Specified expr is printed with newline or space included.

close: Closes an open file: (close file-desc) The file-desc argument is a file descriptor obtained from the open function. After a close, the file descriptor is unchanged but is no longer valid. Data added to an open file is not actually written until the file is closed. The close function returns nil if file-desc is valid, otherwise, it returns an error message.
For example, the following code counts the number of lines in the file somefile.txt and sets the variable ct equal to that number.
backBack top