Author Topic: (challenge) read from file & write to another [newbies]  (Read 4064 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) read from file & write to another [newbies]
« on: January 26, 2004, 09:49:56 AM »
Using the two sub-routines below create a program that will write the contents of one file into another file and print each line to the AutoCAD Text window at the same time. Look at the functions 'read-line' and 'write-line'.

Code: [Select]

;;; this function will prompt the user with a dialog box
;;; to select an existing file to open
;;;
;;; returns the open file handle or nil
;;;
;;; usage
;;; (setq fo (open_file))
(defun open_file (/ file_select open_file)
  (if (setq file_select (getfiled "Select File to Read" "" "txt" 8))
    (setq open_file (open file_select "r"))
    )
  )

Code: [Select]

;;; this function will prompt the user with a dialog box
;;; to enter the name of a new file to write to.
;;;
;;; returns the open file handle or nil
;;;
;;; usage
;;; (setq fw (file_write))
(defun file_write (/ file_select file_open)
  (if (setq file_select (getfiled "Select File to Write" "" "txt" 1))
    (setq file_open (open file_select "w"))
    )
  )
TheSwamp.org  (serving the CAD community since 2003)

deegeecees

  • Guest
(challenge) read from file & write to another [newbies]
« Reply #1 on: January 26, 2004, 09:58:40 AM »
"At the same time" is not possible as the MS OS can only number crunch in 1 dimension.

I would like to participate, but my legs aint what they used to be, and the CAD puter is all the way in the other room.

Yes, I consider myself a newbie.

No, I am not having a "moment".   :P

daron

  • Guest
(challenge) read from file & write to another [newbies]
« Reply #2 on: January 26, 2004, 10:24:44 AM »
Well, you can have the variable that holds the info, pass the info to a princ statement as well as a separate file. This may not be at the same time, exactly, but would sure seem like it to the user.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) read from file & write to another [newbies]
« Reply #3 on: January 26, 2004, 10:25:15 AM »
Quote from: deegeecees
"At the same time" is not possible as the MS OS can only number crunch in 1 dimension.

OK, let me rephrase that. While writing to another file.
TheSwamp.org  (serving the CAD community since 2003)

deegeecees

  • Guest
(challenge) read from file & write to another [newbies]
« Reply #4 on: January 26, 2004, 10:53:52 AM »
I don't mean to pick, I'm just trying to make things interesting. :D

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) read from file & write to another [newbies]
« Reply #5 on: January 30, 2004, 03:10:38 PM »
No takers uh............OooooooKay
Code: [Select]

;;; this function will prompt the user with a dialog box
;;; to select an existing file to open
;;;
;;; returns the open file handle or nil
;;;
;;; usage
;;; (setq fo (open_file))
(defun open_file (/ file_select open_file)
  (if (setq file_select (getfiled "Select File to Read" "" "txt" 8))
    (setq open_file (open file_select "r"))
    )
  )

;;; this function will prompt the user with a dialog box
;;; to enter the name of a new file to write to.
;;;
;;; returns the open file handle or nil
;;;
;;; usage
;;; (setq fw (file_write))
(defun file_write (/ file_select file_open)
  (if (setq file_select (getfiled "Select File to Write" "" "txt" 1))
    (setq file_open (open file_select "w"))
    )
  )


;;; main function
;;; read the contents from one file and write them to another
(defun c:r-w_file (/ read_file write_file each_line)
  (if (setq read_file (open_file))
    (if (setq write_file (file_write))
      (while (setq each_line (read-line read_file))
        (prompt (strcat "\nWriting Line ->" each_line " to file"))
        (write-line each_line write_file)
        ); while
      ); if
    ); if

  (mapcar 'close (list read_file write_file))
 
  (princ)
  )
TheSwamp.org  (serving the CAD community since 2003)