The program below not only shows how you can use a (while) loop to repeat an action until the user right-clicks to exit the loop, but how to do some very fancy text labeling. (setq txt (read-line f)) get the next line in the file. (while (/= nil txt) the end of a file is nothingness. Let’s re-examine the TEXTREAD function and get it to report all of the lines of text in the file it opens: What happens and why? 0A1A2A3A4A5B6B7B8B9Bnil … cycle thru the routine to see. Working with truth conditions: something true is valued T and something false is valued nil: ) the command prompt reports the first string in the fileīut what if we want my TEXTREAD function to read all of the strings in a file? Time to learn all about loops and see how "Booleans" get used to make decisions. ) go open the file and notice that the string has been appended to the original (setq f (open "c:\\test.txt" "a")) ‘append’ mode ) go open the file c:\\test.txt with notepad… the string will be there. (setq f (open "c:\\test.txt" "w")) ‘write’ mode (setq txt (getstring T "\nEnter a sentence: ")) (close f) nil which closes the open file c:\\dave.txt (setq f (open "c:\\dave.txt" "w")) # ready to write content (setq f (findfile "c:/autoexec.bat")) "C:\\autoexec.bat" if the file exists The 'f' variable in this case is called a "file pointer" and it is used to remember the location of the file you are working with. Note again the weird UNIX notation on filenames. We use the (read-line) function to read a line of text from an open file, and we use the (write-line) function to write a line of text to an open file. We use the (close) function to close an open file and you always want to close files you have opened. You can open a file using the "w" parameter to write/overwrite content to the file, or the "a" parameter to open an existing file and append it with content, or the "r" parameter to open the file to read its content. We use the (open) function to create a new file or to open an existing file.
We use the (findfile) function to see if a file already exists. Working with files: AutoLISP can detect, create, open, append, overwrite and delete ASCII texts files. !text1 "You have 5 boxes of stuff in your drawing." (prompt text1) sends the combined string to the Command: prompt (setq text1 (strcat "\nYou have " (itoa total1) " boxes of stuff in your drawing.")) (setq total1 5) 5 which is an integer value, not a string Now do you see why functions like (itoa) are important? In order to make the integer number 5 part of the reporting prompt (which requires a string value only, no numbers are allowed to be passed to the prompt function), we have to convert the number 5 to a string equivalent "5" so that it can be "glued" with the other two strings of the prompt before it is reported at the Command: prompt. Working with prompts: suppose you have a variable named total1 with a numeric (integer) value of 5 stored in it, and suppose you want to report to the user how many boxes of stuff have been found in a drawing. (setq sentence1 (getstring T "\nEnter a sentence: ")) user can use spaces (prompt name1) the name the user entered is shown on the Command: prompt. (setq name1 (getstring "\nEnter a name: ")) user can enter a string value (setq name3 (strcat name1 " " name2)) note the space added between the variables (setq name2 "espinosa-aguilar") "espinosa-aguilar" The (prompt) function spits any string value to the Command: prompt. (getstring) also provides a prompt similar to (getpoint). If a ‘T’ value is provided, the user can enter spaces in the text being entered. (getstring), like (getpoint), also has some parameters to it. (strcat) "glues" strings together into one string. Working with strings: string manipulation can be done with the (strcat), (getstring) and (prompt) functions.