Visual LISP, AutoLISP and General Customization

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

Replacing characters in a text file.

181 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: 4,967
Registered: ‎09-13-2004
Message 2 of 2 (173 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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community