How to create "new line" in txt file

Discussion in 'AutoCAD' started by Adesu, Jan 31, 2005.

  1. Adesu

    Adesu Guest

    I've a script to create a file txt,but I've got trouble to create a "new
    line",here sample

    When entered from VLISP, the prompt function displays a message (a string)
    in the AutoCAD Command window and returns nil to the VLISP Console window.
    The princ, prin1, and print functions all display an expression (not
    necessarily a string) in the AutoCAD Command window and return the
    expression to the VLISP Console window. Optionally, these functions can send
    output to a file. The differences are as follows:

    I want like this

    When entered from VLISPthe prompt function displays a message (a string) in the AutoCAD Command
    window >>>new line
    and returns nil to the VLISP Console windowThe princ, prin1, and print functions all display an expression (not
    necessarily a string) >>>new line
    in the AutoCAD Command window and return the expression to the VLISP
    Console window. >>>new line
    Optionally, these functions can send output to a file. The differences are
    as follows >>>new line


    (setq fol1 (getstring T "\nENTER MAIN FOLDER NAME: "))
    (setq fol2 (getstring T "\nENTER SUB FOLDER NAME: "))
    (setq nam (strcat "c:\\" fol1 "\\" fol2))
    (vl-mkdir nam)
    (setq opt (getstring T "\nENTER FILE NAME: "))
    (setq fill (strcat opt ".txt"))
    (setq namfil (strcat nam "\\" fill))
    (setq fopen (open namfil "w"))
    (setq opt (getstring T "\nENTER NEW TEXT TO FILL IN: "))
    (write-line opt fopen)
    (close fopen)
     
    Adesu, Jan 31, 2005
    #1
  2. Adesu

    ECCAD Guest

    (if (findfile namfil)
    (setq fopen (open namfil "a")); append new line
    (setq fopen (open namfil "w")); start new file
    ); if

    Bob
     
    ECCAD, Jan 31, 2005
    #2
  3. Adesu

    Adesu Guest

    Thanks Bob for your respond,I've modified my code become ,this below

    (setq fol1 (getstring T "\nENTER MAIN FOLDER NAME: "))
    (setq fol2 (getstring T "\nENTER SUB FOLDER NAME: "))
    (setq nam (strcat "c:\\" fol1 "\\" fol2))
    (vl-mkdir nam)
    (setq fn (getstring T "\nENTER FILE NAME: "))
    (setq fil (strcat fn ".txt"))
    (setq nf (strcat nam "\\" fil))
    (setq fopen (open nf "w"))
    (setq opt (getstring T "\nENTER NEW TEXT TO FILL IN: "))
    (setq fopen (open nf "a"))
    (setq fopen (open nf "w"))
    (setq opt1 (getstring T "\nENTER NEW TEXT TO FILL IN: "))
    (write-line opt fopen)
    (write-line opt1 fopen)
    (close fopen)

    Now I've got problem,if last file contained text,and then that code load at
    Visual lisp console editor,last text would replace by new text,or last text
    would remove it.
    My question is how to maintain last (old) text?
     
    Adesu, Feb 1, 2005
    #3
  4. Adesu

    Paul Turvill Guest

    Your code attempts to open file nf three times without ever closing it. You
    can't do that.

    If the file exists and you want to preserve the contents, you must open it
    (once only) with the "a" parameter set, for "append." If the file does not
    exist, or if it exists and you want to overwrite the contents, then you must
    open it with the "w" parameter:

    (if (findfile nf)
    (setq fopen (open nf "a")) ;;open for append
    (setq fopen (open nf "w")) ;;open for (over)write
    ) ;; if

    Put this code right after you get the file name from the user, and remove
    your other (open ...) function calls.
    ___
     
    Paul Turvill, Feb 1, 2005
    #4
  5. Adesu

    Adesu Guest

    Yes Paul & Bob,you are both right,thanks a lot and now I got it.
    here my code
    (setq fol1 (getstring T "\nENTER MAIN FOLDER NAME: "))
    (setq fol2 (getstring T "\nENTER SUB FOLDER NAME: "))
    (setq nam (strcat "c:\\" fol1 "\\" fol2))
    (vl-mkdir nam)
    (setq fn (getstring T "\nENTER FILE NAME: "))
    (setq fil (strcat fn ".txt"))
    (setq nf (strcat nam "\\" fil))
    (if (findfile nf)
    (setq fopen (open nf "a")) ;open for append
    (setq fopen (open nf "w")) ;open for (over)write
    ) ; if
    (setq opt (getstring T "\nENTER NEW TEXT TO FILL IN: "))
    (setq opt1 (getstring T "\nENTER NEW TEXT TO FILL IN: "))
    (write-line opt fopen)
    (write-line opt1 fopen)
    (close fopen)
     
    Adesu, Feb 1, 2005
    #5
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.