reading files with lisp

Discussion in 'AutoCAD' started by kemp, Mar 5, 2004.

  1. kemp

    kemp Guest

    Programmer in training here :) I'm trying to find some sample files that
    show me how to read data from a text file. The file is in the format:
    1,2,3,4
    2,2,3,4
    3,2,3,4

    This is the lisp I am starting with:
    (setq f (open (strcat path fileName) "r"))
    (while
    (setq data (read-line f)))

    Anyone have something similar I could look at to see how to put this into a
    list or manipulate the data properly? Or I hope you can at least point me in
    the right direction and let me know what to search for.

    Thanks a bunch,
    kemp
     
    kemp, Mar 5, 2004
    #1
  2. kemp

    Jeff Mishler Guest

    Well, you are headed in the right direction. Here's an expansion of your
    code that will return a list of lists....turning each line into a list,
    then lists them together.

    (setq f (open (findfile "test.txt") "r"))
    (while (setq data (read-line f))
    (setq data (str2list data ","))
    (setq datalist (cons data datalist))
    )
    (setq datalist (reverse datalist))
    (close f)

    This will return, using your short file sample:

    (("1" "2" "3" "4") ("2" "2" "3" "4") ("3" "2" "3" "4"))

    HTH,
    Jeff

    Oh, yeah, the (str2list) is a function written by John Uhden. It is
    posted in many of the previous messages, if you can't find it here, I
    know it's available at www.cadlantic.com, under the "free stuff".
     
    Jeff Mishler, Mar 5, 2004
    #2
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.