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