Jump to content

import text a variable Autolisp


svorgodne

Recommended Posts

How can I import a TXT file into a variable with autolisp?

 

so far I have been able to do something like this:

 

(setq x (open "C:/Documents and Settings/sgonzalez/My Documents/new_02.txt"))

 

but the result is:

 

 

Command: !x

#

 

instead of the real content of the txt file. Can anybody help me please? Like always, it is urgent

 

Thanks

Link to comment
Share on other sites

(defun iniread (str file)
 ;; iniread
 ;;
 ;; Returns all valid entries in a file. (see below)
 ;;
 ;; file to be read must be structured in a way that 
 ;; all lines begining with a `` ; '' is considered 
 ;; to be a comment.
 ;;
 ;; EX:
 ;; Example file: Example.ini
 ;;
 ;; ; this is an example ini file.
 ;; var1=val1
 ;; var2=val2
 ;; ; var3=val3
 ;; var4=val4
 ;;
 ;;      ( (lambda ( / f my-list )
 ;;          (setq f (open "c:\\Example.ini" "R"))
 ;;          (setq my-list
 ;;              (apply
 ;;                'append
 ;;                   (iniread (read-line f) f)))
 ;;          (close f)
 ;;          my-list ) )
 ;;     
 ;; => ("var1=val1" "var2=val2" "var4=val4")
 ;;
 ;; By: Se7en
 ;;
 ( (lambda (s f)
     (if s
       (cons
         (if (not (wcmatch s "*;*"))
           (list s))
         (iniread (read-line f) f))))
  str
  file ) )

Link to comment
Share on other sites

First, please use code tags in your posts.

 

When opening a file, remember to add the mode (read, write, append), in this situation you would use this:

 

(setq x (open "C:/Documents and Settings/sgonzalez/My Documents/new_02.txt" "[color=red]R[/color][color=black]"[/color]))

 

 

 

Try using the read-line function to get the content you want, then remember to always use the close function for your text file.

 

Hope this helps!

Edited by BlackBox
Forgot about the "quotes"
Link to comment
Share on other sites

Thanks both of you.. So far anyhow, I just forgot to add the mode in the previous message, but I didn't in the programming, so at the end the result is still:

 

 

Command: !x

#

 

Obviously the txt file has a list I want to compare with some other information inside the drawing. On the other hand Se7en, as far as I understand, the only valid programming in your message is:

 

(defun iniread (str file)

( (lambda (s f)

(if s

(cons

(if (not (wcmatch s "*;*"))

(list s))

(iniread (read-line f) f))))

str

file ) )

 

Am I right? the rest is only commented and when I run the "iniread" function this is the result:

 

Command: (iniread)

; error: too few arguments

 

:(

 

Any other ideas?

Thanks in advance again

Link to comment
Share on other sites

Why recursion Se7en? Surely that is going to dump a lot of information on the stack and will bum out pretty quick if the file is of adequate size?

 

(defun _read ( filename / openfile line lst )
 (cond
   ( (setq openfile (open filename "r"))

     (while (setq line (read-line openfile))
       (setq lst (cons line lst))
     )

     (close openfile)

     (reverse lst)
   )
 )
)

Edited by Lee Mac
Link to comment
Share on other sites

Thank you all guys, I just figured it out and it was way too simple here it is the code I used

 

(setq x (open "C:/Documents and Settings/sgonzalez/My Documents/new_02.txt" "r"))

(setq y (read-line x))

(close x)

 

Cheers

Link to comment
Share on other sites

Thank you all guys, I just figured it out and it was way too simple

 

Hooray!

 

And I reiterate...

 

please use code tags in your posts.
Link to comment
Share on other sites

Why recursion Se7en? Surely that is dump a lot of information on the stack and will bum out pretty quick if the file is of adequate size?

 

um, sure, i suppose it could but it would take a pretty big file i suppose. Besides, recursion isnt all evil; *lol* that procedure may be using recursion but it is still really fast. For example, I took yours and mine and pinned them up against each other on a file with 10,000 lines in it and I'm right on your tail.

 

 

;;;;;;;;; --- P R O F I L E   L I S P --- ;;;;;;;;;;;
;;   by (C.) Vladimir Nesterovsky, December 1998.
;;   Free for *non-commercial* personal use only
;;         with this notice unchanged
------------------------------------------------------
TIMER_FOR_LM:READ: n=1, t=0.0100181, t/call=0.0100181.
TIMER_FOR_J7:READ: n=1, t=0.0119895, t/call=0.0119895.

Fastest abstraction: "LM:READ" With a time of: 0.0100181
------------------------------------------------------
...
------------------------------------------------------
TIMER_FOR_LM:READ: n=1, t=0.00901222, t/call=0.00901222.
TIMER_FOR_J7:READ: n=1, t=0.0109836, t/call=0.0109836.

Fastest abstraction: "LM:READ" With a time of: 0.0090122
------------------------------------------------------

Link to comment
Share on other sites

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...