Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!uunet!cme!cam!ARTEMIS From: miller@FS1.cam.nist.gov (Bruce R. Miller) Newsgroups: comp.lang.lisp Subject: Re: multiple use of with-open-file, or with-open-stream Message-ID: <2886773477@ARTEMIS.cam.nist.gov> Date: 24 Jun 91 17:31:17 GMT References: <45084@netnews.upenn.edu> Sender: news@cam.nist.gov Followup-To: comp.lang.lisp Organization: NIST - Computing and Applied Mathematics Laboratory Lines: 37 In article <45084@netnews.upenn.edu>, Jack Vinson writes: > > 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. > > > Jack Vinson vinson@linc.cis.upenn.edu 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 miller@cam.nist.gov