Jump to content

Recommended Posts

Posted

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!

Posted

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,

Posted

Unfortunately this didn't help.. but I've taken another route! Thanks anyway.

Posted

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
)

Posted

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")

Posted

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.

Posted

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.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...