Results 1 to 6 of 6

Thread: open and read text file

  1. #1
    Member
    Join Date
    2000-12
    Posts
    9
    Login to Give a bone
    0

    Default open and read text file

    I need a little assistance with a read-line and while function within a lisp routine. I have a rather long text file that I need to read each line independently, perform a funtion, then move onto the next line and repeat that same function.

    I'm opening the file with this: (setq FIL1 (open "filename" "r"))

    reading the first line - no problem: (read-line FIL1)

    If i'm headed in the right direction how do I make it read line 2 (and the rest sequentially) and make ir do its thing?

  2. #2
    AUGI Addict
    Join Date
    2008-02
    Posts
    1,141
    Login to Give a bone
    0

    Default Re: open and read text file

    Merry Christmas!

    Read and Write to file subroutines...

    Code:
    ;;; Write list to file
    ;;; #File - file to write list to (must be in form "c:\\File.txt")
    ;;; #ListToWrite - list to write to file
    ;;; #Overwrite - If T, will overwrite; nil to append
    ;;; Alan J. Thompson, 04.28.09
    (defun AT:WriteToFile (#File #ListToWrite #Overwrite / #FileOpen)
      (cond ((and (vl-consp #ListToWrite)
                  (setq #FileOpen (open #File
                                        (if #Overwrite
                                          "W"
                                          "A"
                                        ) ;_ if
                                  ) ;_ open
                  ) ;_ setq
             ) ;_ and
             (foreach x #ListToWrite
               (write-line (vl-princ-to-string x) #FileOpen)
             ) ;_ foreach
             (close #FileOpen)
             T
            )
      ) ;_ cond
    ) ;_ defun
    
    
    
    
    ;;; Read contents of file and return list
    ;;; #File - File to read
    ;;; Alan J. Thompson, 11.11.09
    (defun AT:ReadFile (#File / #List #File #Line)
      (and (setq #File (open (findfile #File) "R"))
           (while (setq #Line (read-line #File))
             (setq #List (cons #Line #List))
           ) ;_ while
           (close #File)
      ) ;_ and
      (reverse #List)
    ) ;_ defun

  3. #3
    Member
    Join Date
    2000-12
    Posts
    9
    Login to Give a bone
    0

    Default Re: open and read text file

    Thank - you. I saw that post in an earlier search, but it appeared not to satisfy my needs. Maybe, I'm being a little slow seeing what it is doing.

  4. #4
    AUGI Addict
    Join Date
    2008-02
    Posts
    1,141
    Login to Give a bone
    0

    Default Re: open and read text file

    Quote Originally Posted by bandent View Post
    Thank - you. I saw that post in an earlier search, but it appeared not to satisfy my needs. Maybe, I'm being a little slow seeing what it is doing.
    With comments...

    Code:
    ;;; Read contents of file and return list
    ;;; #File - File to read
    ;;; Alan J. Thompson, 11.11.09
    (defun AT:ReadFile (#File / #List #File #Line)
      (and
        ;; set and open file for reading
        (setq #File (open (findfile #File) "R"))
        ;; each time you issue 'read-line' on a file, it will go to the next line,
        ;; so as long as it will allow me to read-line the file, it will define the value
        ;; to a variable...
        (while (setq #Line (read-line #File))
          ;; value set as #Line, now I'll append it to a list eg. (list "A" "B" "C")
          (setq #List (cons #Line #List))
        ) ;_ while
        ;; close file
        (close #File)
      ) ;_ and
      ;; since I have to append from the front, my list is in reverse, so I just rearrange it.
      (reverse #List)
    ) ;_ defun

  5. #5
    Member
    Join Date
    2000-12
    Posts
    9
    Login to Give a bone
    0

    Default Re: open and read text file

    could explain the AT portion of the defun, I've not run across that one before.

  6. #6
    AUGI Addict
    Join Date
    2008-02
    Posts
    1,141
    Login to Give a bone
    0

    Default Re: open and read text file

    Quote Originally Posted by bandent View Post
    could explain the AT portion of the defun, I've not run across that one before.
    I prefix all my subroutines with AT:
    It's just my subroutine naming convention AT:Name

    AT = Alan Thompson = me

Similar Threads

  1. Open Read-Only: The ability to open a worksharing enabled file in a read only mode.
    By revit.wishlist1942 in forum Revit Architecture - Wish List
    Replies: 5
    Last Post: 2012-09-05, 07:02 PM
  2. Replies: 3
    Last Post: 2012-06-28, 03:51 PM
  3. open a file to read
    By rlash in forum AutoLISP
    Replies: 2
    Last Post: 2010-10-27, 06:17 PM
  4. Make a file read only on open
    By Coolmo in forum VBA/COM Interop
    Replies: 1
    Last Post: 2008-06-11, 09:32 PM
  5. Replies: 3
    Last Post: 2007-07-11, 11:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •