Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Replacing characters in a text file.

1 REPLY 1
Reply
Message 1 of 2
latinman328
707 Views, 1 Reply

Replacing characters in a text file.

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?)

1 REPLY 1
Message 2 of 2
Kent1Cooper
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, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost