Author Topic: Reading a text file and print to alert  (Read 232 times)

0 Members and 1 Guest are viewing this topic.

GDF

  • Water Moccasin
  • Posts: 1989
Reading a text file and print to alert
« on: April 14, 2019, 04:06:11 PM »
I need help in printing the contents of the enclosed text file to an alert dialog box.
Thanks

Code: [Select]
(defun AREA-O  ()
  (setq ff (strcat (getvar "dwgprefix") (getvar "dwgname")))
  (setq ffname (substr ff 1 (- (strlen ff) 4)))
  (setq fn (strcat ffname ".txt"))
  (setq file3 (open fn "r"))
  (alert (read-line file3))
  (close fn)
  (princ))
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2019x64 Windows 10x64

Jeff_M

  • King Gator
  • Posts: 3942
  • C3D user & customizer
Re: Reading a text file and print to alert
« Reply #1 on: April 14, 2019, 04:45:56 PM »
Something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun AREA-O  ()
  2.   (setq ff (strcat (getvar "dwgprefix") (getvar "dwgname")))
  3.   (setq ffname (substr ff 1 (- (strlen ff) 4)))
  4.   (setq fn (strcat ffname ".txt"))
  5.   (setq file3 (open fn "r")
  6.         txt ""
  7.         )
  8.   (while (setq ln (read-line file3))
  9.     (setq txt (strcat txt "\n" ln))
  10.     )
  11.   (close file3)
  12.   (alert txt)
  13.   (princ)
  14.   )
  15.  

GDF

  • Water Moccasin
  • Posts: 1989
Re: Reading a text file and print to alert
« Reply #2 on: April 14, 2019, 04:54:26 PM »
Thanks Jeff
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2019x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12241
  • London, England
Re: Reading a text file and print to alert
« Reply #3 on: April 14, 2019, 05:06:13 PM »
You might want to verify that the file exists & may be opened, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (defun area-o2 ( / des lst str txt )
  2.     (if (and (setq txt (findfile (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".txt")))
  3.              (setq des (open txt "r"))
  4.         )
  5.         (progn
  6.             (while (setq str (read-line des))
  7.                 (setq lst (vl-list* "\n" str lst))
  8.             )
  9.             (close des)
  10.             (alert (apply 'strcat (reverse (cdr lst))))
  11.         )
  12.         (princ (strcat "\n" txt " not found."))
  13.     )
  14.     (princ)
  15. )

GDF

  • Water Moccasin
  • Posts: 1989
Re: Reading a text file and print to alert
« Reply #4 on: April 14, 2019, 05:45:58 PM »
Thanks Lee

Here is my complete routine hacked together. It creates rectangles with a Reactor  text link.
See enclosed file. Yes, it's a crude routine!

Just remark out these functions:
(ARCH:F_S-VAR) ;;save vars
(ARCH:F_F-VAR) ;;reset vars
(ARCH:LYR "AREA") ;;create layer 
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2019x64 Windows 10x64