Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Write list to file - LISP

4 REPLIES 4
Reply
Message 1 of 5
eagledon
799 Views, 4 Replies

Write list to file - LISP

This is a MODIFIED piece of code I found here:
http://discussion.autodesk.com/thread.jspa?messageID=1168520

Using AutoLISP, I need to be able to write a list to file, but this code does not seem to work.
Any tips?

(defun C:EXPRT2F ( / fname selset txt cnt nme)
(setq fname "c:\lsptest.txt")
(setq selset '(a b 2 5 d 9 2 kd 9 8))
(setq txt (open fname "w") cnt 0)
(repeat (length selset)
(setq nme (nth cnt selset))
(write-line nme txt)
(setq cnt (1+ cnt)) )
(close txt) )
4 REPLIES 4
Message 2 of 5
rogerio_brazil
in reply to: eagledon

Hi eagledon,

Two errors:

(setq fname "c:\lsptest.txt");;error ( c:\\, not c:\)
(setq fname "c:\\lsptest.txt");;ok

(setq selset '(a b 2 5 d 9 2 kd 9 8));error
(setq selset '("a" "b" "2" "5" "9" "2" "kd" "9" "8"))

(defun C:EXPRT2F ( / fname selset txt cnt nme)
(setq fname "c:\\lsptest.txt")
(setq selset '("a" "b" "2" "5" "9" "2" "kd" "9" "8"))
(setq txt (open fname "w") cnt 0)
(repeat (length selset)
(setq nme (nth cnt selset))
(write-line nme txt)
(setq cnt (1+ cnt)) )
(close txt) )

Try it now

:)

Rogerio
Message 3 of 5
Anonymous
in reply to: eagledon

Hi eagledon,

Here is an old trick to write/read a list to/from a file. As you can see
this keeps the list as a list.

(defun write_list_to_file (my_list / file_handle)
(setq file_handle (open "c:\\lsptest.txt") "w"))
(prin1 my_list file_handle)
(close file_handle)
(princ)
)

(defun read_list_from_file ( / file_handle my_list)
(setq file_handle (open "c:\\lsptest.txt" "r"))
(setq my_list (read-line file_handle))
(close file_handle)
my_list
)

If you want it each element on a new line then use the next one:

(defun C:EXPRT2F ( / file_handle selset counter name)
(setq file_handle (open "c:\\lsptest.txt" "w"))
(setq selset '(a b 2 5 d 9 2 kd 9 8))
(setq counter 0)
(repeat (length selset)
(setq name (nth counter selset))
(if (= counter 0)
(princ name file_handle)
(print name file_handle)
)
(setq counter (1+ counter))
)
(close file_handle)
(princ)
)

Regards,

Constantin

a écrit dans le message de news:
5259839@discussion.autodesk.com...
This is a MODIFIED piece of code I found here:
http://discussion.autodesk.com/thread.jspa?messageID=1168520

Using AutoLISP, I need to be able to write a list to file, but this code
does not seem to work.
Any tips?

(defun C:EXPRT2F ( / fname selset txt cnt nme)
(setq fname "c:\lsptest.txt")
(setq selset '(a b 2 5 d 9 2 kd 9 8))
(setq txt (open fname "w") cnt 0)
(repeat (length selset)
(setq nme (nth cnt selset))
(write-line nme txt)
(setq cnt (1+ cnt)) )
(close txt) )
Message 4 of 5
Anonymous
in reply to: eagledon

I forgot to tell that when reading a list from a file
with (read_list_from_file) the returned value is a
string. Although this can be of some interest for
some particular situations, like passing it to an

(alert (read_list_from_file))

call for debugging purposes, you will need to use

(read (read_list_from_file))

in order to get a real list.


"Constantin Gherasim" a écrit dans le
message de news: 5260704@discussion.autodesk.com...
Hi eagledon,

Here is an old trick to write/read a list to/from a file. As you can see
this keeps the list as a list.

(defun write_list_to_file (my_list / file_handle)
(setq file_handle (open "c:\\lsptest.txt") "w"))
(prin1 my_list file_handle)
(close file_handle)
(princ)
)

(defun read_list_from_file ( / file_handle my_list)
(setq file_handle (open "c:\\lsptest.txt" "r"))
(setq my_list (read-line file_handle))
(close file_handle)
my_list
)

If you want it each element on a new line then use the next one:

(defun C:EXPRT2F ( / file_handle selset counter name)
(setq file_handle (open "c:\\lsptest.txt" "w"))
(setq selset '(a b 2 5 d 9 2 kd 9 8))
(setq counter 0)
(repeat (length selset)
(setq name (nth counter selset))
(if (= counter 0)
(princ name file_handle)
(print name file_handle)
)
(setq counter (1+ counter))
)
(close file_handle)
(princ)
)

Regards,

Constantin

a écrit dans le message de news:
5259839@discussion.autodesk.com...
This is a MODIFIED piece of code I found here:
http://discussion.autodesk.com/thread.jspa?messageID=1168520

Using AutoLISP, I need to be able to write a list to file, but this code
does not seem to work.
Any tips?

(defun C:EXPRT2F ( / fname selset txt cnt nme)
(setq fname "c:\lsptest.txt")
(setq selset '(a b 2 5 d 9 2 kd 9 8))
(setq txt (open fname "w") cnt 0)
(repeat (length selset)
(setq nme (nth cnt selset))
(write-line nme txt)
(setq cnt (1+ cnt)) )
(close txt) )
Message 5 of 5
Anonymous
in reply to: eagledon

Oops, there is an error on this one:

(defun write_list_to_file (my_list / file_handle)
(setq file_handle (open "c:\\lsptest.txt") "w"))
(prin1 my_list file_handle)
(close file_handle)
(princ)
)

and it should be:

(defun write_list_to_file (my_list / file_handle)
(setq file_handle (open "c:\\lsptest.txt" "w"))
(prin1 my_list file_handle)
(close file_handle)
(princ)
)

Sorry,

Constantin



"Constantin Gherasim" a écrit dans le
message de news: 5260704@discussion.autodesk.com...
Hi eagledon,

Here is an old trick to write/read a list to/from a file. As you can see
this keeps the list as a list.

(defun write_list_to_file (my_list / file_handle)
(setq file_handle (open "c:\\lsptest.txt") "w"))
(prin1 my_list file_handle)
(close file_handle)
(princ)
)

(defun read_list_from_file ( / file_handle my_list)
(setq file_handle (open "c:\\lsptest.txt" "r"))
(setq my_list (read-line file_handle))
(close file_handle)
my_list
)

If you want it each element on a new line then use the next one:

(defun C:EXPRT2F ( / file_handle selset counter name)
(setq file_handle (open "c:\\lsptest.txt" "w"))
(setq selset '(a b 2 5 d 9 2 kd 9 8))
(setq counter 0)
(repeat (length selset)
(setq name (nth counter selset))
(if (= counter 0)
(princ name file_handle)
(print name file_handle)
)
(setq counter (1+ counter))
)
(close file_handle)
(princ)
)

Regards,

Constantin

a écrit dans le message de news:
5259839@discussion.autodesk.com...
This is a MODIFIED piece of code I found here:
http://discussion.autodesk.com/thread.jspa?messageID=1168520

Using AutoLISP, I need to be able to write a list to file, but this code
does not seem to work.
Any tips?

(defun C:EXPRT2F ( / fname selset txt cnt nme)
(setq fname "c:\lsptest.txt")
(setq selset '(a b 2 5 d 9 2 kd 9 8))
(setq txt (open fname "w") cnt 0)
(repeat (length selset)
(setq nme (nth cnt selset))
(write-line nme txt)
(setq cnt (1+ cnt)) )
(close txt) )

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost