From: Xaveria Xanthe
Subject: Simple Array/File Question
Date: 
Message-ID: <fc09e7b5.0204132052.1918e8f8@posting.google.com>
Hello

Just a simple question&#8230;

I have an array in common lisp that I want to be able to write to a
file, and then read later.
I&#8217;ve been trying to use the &#8216;write&#8217; and
&#8216;read&#8217; command, but I can&#8217;t seem to get the syntax
on either of them.  Would someone mind showing me how it should be
done?

Thanks
-Xaveria

From: Lieven Marchand
Subject: Re: Simple Array/File Question
Date: 
Message-ID: <m34riejwky.fsf@localhost.localdomain>
·············@hotmail.com (Xaveria Xanthe) writes:

> Hello
> 
> Just a simple question&#8230;
> 
> I have an array in common lisp that I want to be able to write to a
> file, and then read later.
> I&#8217;ve been trying to use the &#8216;write&#8217; and
> &#8216;read&#8217; command, but I can&#8217;t seem to get the syntax
> on either of them.  Would someone mind showing me how it should be
> done?

For a simple answer, try

(let ((*print-readably* t))
   (write your-array))

In general, writing arrays in CL so that an equivalent array will be
read in is not so trivial. It makes a difference whether you only want
to get the "same" [0] array when read by the identical implementation
that wrote it or want to be able to read in the array by another
implementation. Things to consider are fill pointers, upgraded array
element types and the available specialized array types of the
implementation.

[0] for some equivalence relation

-- 
Lieven Marchand <···@wyrd.be>
She says, "Honey, you're a Bastard of great proportion."
He says, "Darling, I plead guilty to that sin."
Cowboy Junkies -- A few simple words
From: Robert Folland
Subject: Re: Simple Array/File Question
Date: 
Message-ID: <u662unow3.fsf@circinus.no>
·············@hotmail.com (Xaveria Xanthe) writes:

> I have an array in common lisp that I want to be able to write to a
> file, and then read later.
> I&#8217;ve been trying to use the &#8216;write&#8217; and
> &#8216;read&#8217; command, but I can&#8217;t seem to get the syntax
> on either of them.  Would someone mind showing me how it should be
> done?

I think 'print' and 'read' will probably work for you.

-Robert
From: Robert Folland
Subject: Re: Simple Array/File Question
Date: 
Message-ID: <u1ydinogm.fsf@circinus.no>
Robert Folland <······@circinus.no> writes:

> I think 'print' and 'read' will probably work for you.

Something like

(defun write-and-read-array ()
  (let ((my-array '#(1 2 3 4 5)))
    (with-open-file (stream "testfile.dat" :direction :output
			    :if-exists :supersede)
		    (print my-array stream))
    (setf my-array nil)
    (with-open-file (stream "testfile.dat")
		    (setf my-array (read stream)))
    my-array))

-Robert
From: Thomas A. Russ
Subject: Re: Simple Array/File Question
Date: 
Message-ID: <ymi8z7ncmwb.fsf@sevak.isi.edu>
Robert Folland <······@circinus.no> writes:

> 
> Robert Folland <······@circinus.no> writes:
> 
> > I think 'print' and 'read' will probably work for you.
> 
> Something like:

Note that it may be necessary to bind *print-array* to T in order
to make sure that print will actually print something readable.  Binding
*print-readably* to T is also probably a good idea, at least around the
PRINT statement.  Some lisps start out with *print-array* set to NIL,
which would cause problems.

(defun write-and-read-array ()
  (let ((my-array '#(1 2 3 4 5)))
    (with-open-file (stream "testfile.dat" :direction :output
			    :if-exists :supersede)
         (let ((*print-array* t)
	       (*print-readably* t))
	    (print my-array stream)))

     (setf my-array nil)
     (with-open-file (stream "testfile.dat")
 		    (setf my-array (read stream)))
     my-array))
 


-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu