Writing to & Reading a file.

Discussion in 'AutoCAD' started by Rick Keller, Jun 15, 2004.

  1. Rick Keller

    Rick Keller Guest

    I have a file that I have created.

    Example...

    GCL 0
    CSJ 0
    BCW 0

    How can I open the file and increase the number by 1.

    I know how to read the file and find the line I want but how do you change 1
    line in a file.

    Do I have to overwrite the file? If so how can I read the entire file to
    change it so I can re-write it.

    Thanks
    Rick
     
    Rick Keller, Jun 15, 2004
    #1
  2. Rick Keller

    ECCAD Guest

    To increment the number, you need to make it a number (is a string when you read it). Then increment it.
    e.g.
    (setq rd_lin (read-line in_file))
    ...get the number part
    (setq N (substr rd_lin 5 3)); read 3 char's case it > 100
    (setq N (atoi N)); make it a number
    (setq N (+ N 1)); and increment it
    (setq N (itoa N)); convert back to string
    .....
    Yes, you have to 'rewrite' the entire file.
    Basic steps:
    (while (rd_line..
    (setq entire_file (cons rd_lin entire_file))
    ...
    (if (entire_file
    (setq entire_file (reverse entire_file)); flip it around
    )
    (close in_file)
    ... then write it back out
    (setq ctr 0)
    (repeat (length entire_file))
    (setq Output (nth ctr entire_file))
    (write-line O Output_file)
    (setq ctr (+ ctr 1))
    ))
    (close output_file)
    ...

    Bob
     
    ECCAD, Jun 15, 2004
    #2
  3. Rick Keller

    Rick Keller Guest

    Thanks for the reply I'll have to decipher what it all means.

    There are alot of new functions that I haven't used before.

    Thanks
    Rick
     
    Rick Keller, Jun 15, 2004
    #3
  4. Rick Keller

    bruno Guest

    Rick Keller a écrit :


    (setq lst (read-file filename))
    ;; --> ("GCL\t0" "CSJ\t0" "BCW\t0")

    (setq lst (mapcar 'string->assoc lst))
    ;; --> (("GCL" . "0") ("CSJ" . "0") ("BCW" . "0"))

    (setq ass (assoc "CSJ" lst))
    ;; --> ("CSJ" . "0")

    (setq N+1 (itoa (1+ (atoi(cdr ass)))))
    ;; --> "1"

    (setq lst (subst (cons "CSJ" N+1) ass lst))
    ;; --> (("GCL" . "0") ("CSJ" . "1") ("BCW" . "0"))

    (setq lst (mapcar 'assoc->string lst))
    ;; --> ("GCL\t0" "CSJ\t1" "BCW\t0")

    (write-file filename lst)




    functions:


    (defun read-file (filename / f line lst)
    (setq f (open filename "r"))
    (while (setq line (read-line f))
    (setq lst (append lst (list line)))
    )
    (close f)
    lst
    )
    (defun write-file (filename lst / f)
    (setq f (open filename "w"))
    (mapcar '(lambda (x) (princ x f) (princ "\n" f)) lst)
    (close f)
    )

    ;; (string->assoc "aze\t0") ==> '("aze" . "0")
    (defun string->assoc (str)
    (setq c 0)
    (while (and (/= "\t" (setq cr (substr str (setq c (1+ c)) 1))) (/= ""
    cr)))
    (cons (substr str 1 (1- c)) (substr str (1+ c)))
    )
    ;; (assoc->string '("aze" . "0")) ==> "aze\t0"
    (defun assoc->string (ass)
    (strcat (car ass) "\t" (cdr ass))
    )
    you're welcome
     
    bruno, Jun 16, 2004
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.