multiple use of with-open-file, or with-open-stream 
Author Message
 multiple use of with-open-file, or with-open-stream

Hello again lisp.hackers,

I am reading a bunch of data into lisp from a number of files which isn't that
big of a deal, I can simply nest several with-open-file's.  The problem occurs
when I do not know, a priori, the number of data files from which I will be
reading.  i.e. That number is read in as well.

Is there some way (a macro?) that I can get the functionality of with-open-file
and with-open-stream without initially knowing how many files (or stream) I
will have initially?  My current solution is to open all the files, run the
body of the program, and then close everything.  This is fine, until my program
bombs (and it's kinda large) and I have to remember to close out all the
streams by hand.




Fri, 10 Dec 1993 22:16:42 GMT  
 multiple use of with-open-file, or with-open-stream

Quote:
> Is there some way (a macro?) that I can get the functionality of
> with-open-file and with-open-stream without initially knowing how many
> files (or stream) I will have initially?  My current solution is to
> open all the files, run the body of the program, and then close
> everything.  This is fine, until my program bombs (and it's kinda
> large) and I have to remember to close out all the streams by hand.

The primitive used by (with-open-file) to close things in the face of
errors is (unwind-protect).  That's easy enough to use.  Here's a
trivial function that opens an arbitrary number of files and causes an
error.  It automatically closes the files even if you abort out of the
error.

(defun open-many-files (&rest files)
  (let ((streams nil))
    (unwind-protect
      (progn
        (dolist (file files)
          (push (open file) streams))
        (error "Too bad"))
     ;;Here's the cleanup form.  Close all the files...
     (dolist (stream streams)
       (close stream)))))

 - Peter
--

   Peter L. DeWolf
   Motorola Cambridge Research Center



Sat, 11 Dec 1993 04:00:12 GMT  
 multiple use of with-open-file, or with-open-stream

Quote:

> Hello again lisp.hackers,

> I am reading a bunch of data into lisp from a number of files which isn't that
> big of a deal, I can simply nest several with-open-file's.  The problem occurs
> when I do not know, a priori, the number of data files from which I will be
> reading.  i.e. That number is read in as well.

> Is there some way (a macro?) that I can get the functionality of with-open-file
> and with-open-stream without initially knowing how many files (or stream) I
> will have initially?  My current solution is to open all the files, run the
> body of the program, and then close everything.  This is fine, until my program
> bombs (and it's kinda large) and I have to remember to close out all the
> streams by hand.



It's not too clear what you're doing with all those files, but I'll try
to offer some alternatives.
  You seem to want them all open while the program is running. Could you
alternatively, read them one at a time into some sort of data structure?

  Otherwise, you could use recursion; recurse until all files are open
then within that do whatever you want.  But what do you do with the
streams, though? collect them into a list? If so, then consider:

  with-open-file/stream use an unwind-protect to achieve the
automatic closing.  You also could use unwind-protect; within the
`protected-form' you would loop, open each file, collect the streams
into a list.  then do whatever you want with the streams. The
`cleanup-forms' would loop through opened streams and close them.

bruce



Sat, 11 Dec 1993 01:31:17 GMT  
 multiple use of with-open-file, or with-open-stream
You should use this idiom:

(let ((abort t)
      (list-of-file-to-open (find-files-to-open))
      (list-of-streams-to-close ()))
  (unwind-protect
      (progn
        (dolist (file list-of-files-to-open)
          (push (open-my-file file) list-of-streams-to-close))
        (do-stuff-with-streams list-of-streams-to-close)
        (setq abort nil))
    (dolist (stream list-of-streams-to-close)
      (close stream :abort abort))))

If you bomb out in the middle of do-stuff-with-streams then all the files
will be closed properly.

If you macroexpand with-open-file you will find that it uses the same idiom
only with one file and stream.



Fri, 10 Dec 1993 17:11:16 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help on opening multiple input files(test vector files)

2. SUMMARY : using Fortran OPEN with STREAM qualifier on Ultrix

3. File dialog for opening multiple files?

4. Selecting multiple files in the file open dialog

5. Help with opening mixed stream file

6. File Open, or not Open, question

7. error 37 File not open using a variable file name

8. OPEN(VIEW) without OPEN(FILE)

9. Opening a file already open

10. opening existing files with OPEN()

11. opening a file opens it in Notepad

12. opening 'file open' dialog from console

 

 
Powered by phpBB® Forum Software