Writing and reading a list from a file

Discussion in 'AutoCAD' started by Jim Nelson, Mar 23, 2005.

  1. Jim Nelson

    Jim Nelson Guest

    (using AutoDesk Map 3D 2005)

    If I write a list out to a file with something like:

    (setq mylist (list 1.2 1.3))
    (setq fn (open "c:\\listfile.txt" "w"))
    (princ mylist fn)
    (setq fn (close fn))

    ....and then retrieve the list with something like:

    (setq fn (open "c:\\listfile.txt" "r"))
    (setq mylist (read-line fn))
    (setq fn (close fn))

    ....the value of mylist is now a string "(1.2 1.3)"

    Is there any way to turn it back into a list that looks like (1.2 1.3) ?

    Thanks

    Jim
     
    Jim Nelson, Mar 23, 2005
    #1
  2. Jim Nelson

    Paul Turvill Guest

    (setq mylist (read (read-line (n)))
    ___
     
    Paul Turvill, Mar 23, 2005
    #2
  3. Use the read function.
     
    Dale Levesque, Mar 23, 2005
    #3
  4. First, you are using the wrong function to write
    the data (you must use (print), not (princ), siince
    the former will not expand strings).

    So, the output expression would be:

    (print <list expression> <file>)

    The input expression would be:

    (read (read-line <file>))

    Anther thing you need to beware of is that if
    your data has real numbers in it, they will be
    rounded off to the 5th or 6th place, resulting
    in a loss of precision.
     
    Tony Tanzillo, Mar 24, 2005
    #4
  5. Jim Nelson

    Jim Nelson Guest

    Thank you all for your help!

    Jim
     
    Jim Nelson, Mar 24, 2005
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.