From: piscesboy on
I have a recursive function that writes out to a file using (with-open-
file). I pass a pointer to the file to be written as a parameter to
recursive calls to the function. But if the file is already open I
don't recursive calls to (with-open-file) to open it again. I need a
way to test if the file pointer is already open and if it is, to
simply use it to write to the file.

Maybe there is a better way to do this altogether...?
From: sds on
On Jan 31, 11:12 am, piscesboy <oraclmas...(a)gmail.com> wrote:
> I have a recursive function that writes out to a file using (with-open-
> file). I pass a pointer to the file to be written as a parameter to
> recursive calls to the function. But if the file is already open I
> don't recursive calls to (with-open-file) to open it again. I need a
> way to test if the file pointer is already open and if it is, to
> simply use it to write to the file.

I am not sure what a "file pointer" is, but you might find OPEN-STREAM-
P useful:
http://www.lispworks.com/documentation/HyperSpec/Body/f_open_s.htm
From: piscesboy on
On Jan 31, 1:22 pm, sds <sam.steing...(a)gmail.com> wrote:
> On Jan 31, 11:12 am, piscesboy <oraclmas...(a)gmail.com> wrote:
>
> > I have a recursive function that writes out to a file using (with-open-
> > file). I pass a pointer to the file to be written as a parameter to
> > recursive calls to the function. But if the file is already open I
> > don't recursive calls to (with-open-file) to open it again. I need a
> > way to test if the file pointer is already open and if it is, to
> > simply use it to write to the file.
>
> I am not sure what a "file pointer" is, but you might find OPEN-STREAM-
> P useful:http://www.lispworks.com/documentation/HyperSpec/Body/f_open_s.htm

I guess that's the C programmer in me talking. Thanks.
From: Paul Donnelly on
piscesboy <oraclmaster(a)gmail.com> writes:

> I have a recursive function that writes out to a file using (with-open-
> file). I pass a pointer to the file to be written as a parameter to
> recursive calls to the function. But if the file is already open I
> don't recursive calls to (with-open-file) to open it again. I need a
> way to test if the file pointer is already open and if it is, to
> simply use it to write to the file.
>
> Maybe there is a better way to do this altogether...?

Yeah, either drop the recursion and do your writing in a loop:

(with-open-file (foo "file")
(loop write to foo))

Or have two functions, one for the external interface and one for
the loop:

(defun write-out ()
(with-open-file (foo "file")
(write-out1 foo)))

(defun write-out1 (stream)
write some stuff to stream
(write-out1 stream))

If you've got initialization work to do, don't test whether you need to
redo it every time through the loop. It may not be a bottleneck,
especially since you're doing file IO, but come on.
From: Barry Margolin on
In article <87aavux87t.fsf(a)plap.localdomain>,
Paul Donnelly <paul-donnelly(a)sbcglobal.net> wrote:

> piscesboy <oraclmaster(a)gmail.com> writes:
>
> > I have a recursive function that writes out to a file using (with-open-
> > file). I pass a pointer to the file to be written as a parameter to
> > recursive calls to the function. But if the file is already open I
> > don't recursive calls to (with-open-file) to open it again. I need a
> > way to test if the file pointer is already open and if it is, to
> > simply use it to write to the file.
> >
> > Maybe there is a better way to do this altogether...?
>
> Yeah, either drop the recursion and do your writing in a loop:
>
> (with-open-file (foo "file")
> (loop write to foo))
>
> Or have two functions, one for the external interface and one for
> the loop:
>
> (defun write-out ()
> (with-open-file (foo "file")
> (write-out1 foo)))
>
> (defun write-out1 (stream)
> write some stuff to stream
> (write-out1 stream))
>
> If you've got initialization work to do, don't test whether you need to
> redo it every time through the loop. It may not be a bottleneck,
> especially since you're doing file IO, but come on.

You could do it all in one function:

(defun write-out (... &optional stream)
(flet ((write-out-internal (s)
write stuff to s
;; and recurse
(write-out ... stream)))
(if stream
(write-out-internal s)
(with-open-file (new-stream...)
(write-out-internal ... new-stream)))))

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***