scubastu Posted October 12, 2009 Posted October 12, 2009 Hi friends, I have this error and its confusing me.. Its simple.. I'm certain. I have already written two .txt files as the first part of my lisp and now want to read them back in to use data for calculations in second part of lisp. This is where the ; error: bad argument type: stringp nil comes in. Maybe a system variable I haven't thought of? or do I need a pause between closing and reading? Here's the Lisp (close dist) ;end write-line distances.txt (close total) ;end write-line total (setq txtfile (findfile "distances.txt")) ;find distances file (setq f (open txtfile "r")) ; open for reading (setq dtf (read-line f)) ;read firstline (while (/= dtf "EOF") ;continue reading until end of file (setq dtf (read-line f)) ) (close f) ) The Distance file is a txt file with 3 distances on each line 2.0000 , 2.5000 and 3.0942 (just a test file for bigger grander lisp) The reason I'm writing to file is I couldn't figure out how store each length of line and the total length of all lines selected into different variables but by only selecting them once and in order.. that's another story! Thanks in advanced! Quote
MSasu Posted October 13, 2009 Posted October 13, 2009 Excepting the case when you place explicitly the string “EOF” at the last line of your file you must compare the result of read-line with nil to see when reach the end of file – this is the way used by AutoLISP to notify that there are no more lines to read. So, your code should look like: (while (/= dtf nil) ;continue reading until end of file (setq dtf (read-line f)) ) or: (while dtf ;continue reading until end of file (setq dtf (read-line f)) ) Regards, Quote
scubastu Posted October 14, 2009 Author Posted October 14, 2009 Unfortunately this didn't help.. but I've taken another route! Thanks anyway. Quote
MSasu Posted October 14, 2009 Posted October 14, 2009 Not sure why you say that doesn’t work – please take a look on the below code: (defun ReadTextFile( theFilePath / FileID FileLine FileContent ) (if (findfile theFilePath) ;validate file existance (progn (setq FileID (open theFilePath "r")) ;open file for read operation (while (setq FileLine (read-line FileID)) ;parse content until EOF (setq FileContent (append FileContent ;ans store lines as a list (list FileLine))) ) (close FileID) ;close argument file ) ) FileContent ;return file content or nil ) Quote
jammie Posted October 14, 2009 Posted October 14, 2009 Nice code msasu, The only other thing to point out is when entering the path use either a double backslash or a single forwardslash (ReadTextFile "c:[color="Red"]\\[/color]Temp.txt") (ReadTextFile "c:[color="Red"]/[/color]Temp.txt") Quote
scubastu Posted October 14, 2009 Author Posted October 14, 2009 OK all go! Thanks again! I think I needed the full path in the open statement the variable didn't work as it only stored the name not the path?! sound right? Its in a different directory than support. Quote
MSasu Posted October 15, 2009 Posted October 15, 2009 Just take a look on "AutoLISP Reference Guide > AutoLISP Functions > F Functions > findfile" in AutoCAD’s help to clarify the way it use to locate files. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.