Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
New Member
latinman328
Posts: 1
Registered: ‎09-13-2012
Message 1 of 2 (205 Views)

Replacing characters in a text file.

205 Views, 1 Replies
09-13-2012 01:16 PM

Replacing characters in a text file.
Dear all:
I solved the following Lisp routine chore. I have this csv file of coordinates (and many more) that comes from a total station:1,100,100,10,50
2,100,200,12,50
3,200,200,12,50
4,200,100,10,51
and should replace the code after the last comma like this:1,100,100,10,.50
2,100,200,12,.50
3,200,200,12,.50
4,200,100,10,..50
To be able to generate the topographic map.
And I have this code but does not work properly:
(defun c:cod (/ filename fileopenr readline)
(if (setq filename (getfiled "!!! SELECT *.csv FILE COORDINATE:... ¡¡¡" "c:/" "csv" 0 ))
(progn (setq fileopenr (open filename "w"))
(while (setq readline (read-line fileopenr))
(while (vl-string-position ",50" readline)
(setq readline (vl-string-subst ",.50" ",50" readline))
)
)
)
)
)
)
(write-line readline fileopenr)
(close filename)

So please help me debug Lisp code (or Visual Lisp or VBA?)

*Expert Elite*
Kent1Cooper
Posts: 5,879
Registered: ‎09-13-2004
Message 2 of 2 (197 Views)

Re: Replacing characters in a text file.

09-13-2012 02:22 PM in reply to: latinman328

latinman328 wrote:

.... I have this csv file of coordinates (and many more) that comes from a total station:1,100,100,10,50
2,100,200,12,50
3,200,200,12,50
4,200,100,10,51
and should replace the code after the last comma like this:1,100,100,10,.50
2,100,200,12,.50
3,200,200,12,.50
4,200,100,10,..50
....
And I have this code but does not work properly:
....

(progn (setq fileopenr (open filename "w"))

(while (setq readline (read-line fileopenr))
(while (vl-string-position ",50" readline)
(setq readline (vl-string-subst ",.50" ",50" readline))
)
)
)
)
)
)
(write-line readline fileopenr)
(close filename)

....


You can't read from and write to the same file.  If you open a file for writing, and the file name exists, it overwrites it with an initially empty file.  If you want to write something to a file that already exists, without wiping out its current content, I think your only choice is the "append" option, which will add to the end but won't replace lines already in the file.  I think you will need two files for this, to read from one and write to another.  [Also, (close) should take the variable name that the file name returned by the (open) function was saved into, rather than the file name itself.]  Try something like this [untested]:

 

(defun ....
  (if ....

    (progn

      (setq

        fileR (open filename "r")

        fileW (open {...build edited-file path and name...} "w")

      ); setq

      (while

        (setq readline (read-line fileR))
        (while (vl-string-position ",50" readline)

          ;; [I assume (while) instead of (if) is in case there's more than one ",50" in a line]
          (setq readline (vl-string-subst ",.50" ",50" readline))

        ); while

        (write-line readline fileW); put into new file, whether edited or not, so all lines will be in it

      ); while
    ); progn
  ); if
  (close fileR)

  (close fileW)

); defun

Kent Cooper
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.