From: Peder Y
Subject: Saving a variable to a file
Date: 
Message-ID: <10b28c6c.0210281930.15d649d2@posting.google.com>
Understanding the hyperspec on file io wasn't trivial, so here is
probably a simple question.

Say I have a list foo that contains some important data. I want to
retrieve this data next time I start my program. For that reason I
need to put this data on a file for safekeeping. Does anyone have an
example of such io?

- Peder -

From: Christopher C. Stacy
Subject: Re: Saving a variable to a file
Date: 
Message-ID: <uhef5dhdv.fsf@dtpq.com>
>>>>> On 28 Oct 2002 19:30:04 -0800, Peder Y ("Peder") writes:

 Peder> Understanding the hyperspec on file io wasn't trivial, so here is
 Peder> probably a simple question.

 Peder> Say I have a list foo that contains some important data. I want to
 Peder> retrieve this data next time I start my program. For that reason I
 Peder> need to put this data on a file for safekeeping. Does anyone have an
 Peder> example of such io?

Suppose your data is in a variable called DATA.
You could just print it out to a file:

 (with-open-file (out "my-data.dat" :direction :out)
    (print data out))

Then later you could just read it back in:

 (with-open-file (in "my-data.dat")
   (setq data (read in)))

If you reach a point where you understand that
and it's not sufficient for your needs, ask again.
From: Eli Bendersky
Subject: Re: Saving a variable to a file
Date: 
Message-ID: <yqcr8e9zkm8.fsf@lnx-baruch.haifa.ibm.com>
The correct :direction is :output

-- 
Email: ________ at yahoo dot com
(the username is spur4444)
From: Christopher C. Stacy
Subject: Re: Saving a variable to a file
Date: 
Message-ID: <u4rb41k7f.fsf@dtpq.com>
>>>>> On 29 Oct 2002 10:59:11 +0200, Eli Bendersky ("Eli") writes:

 Eli> The correct :direction is :output

Right.
From: Nils Goesche
Subject: Re: Saving a variable to a file
Date: 
Message-ID: <87u1j5c3fa.fsf@darkstar.cartan>
·······@ec.auckland.ac.nz (Peder Y) writes:

> Understanding the hyperspec on file io wasn't trivial, so here
> is probably a simple question.
> 
> Say I have a list foo that contains some important data. I want to
> retrieve this data next time I start my program. For that reason I
> need to put this data on a file for safekeeping. Does anyone have an
> example of such io?

This is not entirely trivial because it is not a priori clear how
to save some complicated data structures.  However, in simple
cases you can get away with just using the builtin printed
representation of objects:

The function PRIN1 will print objects with escape characters so
they can be read in again by READ:

CL-USER 1 > (prin1 '(foo 2 "17"))
(FOO 2 "17")
(FOO 2 "17")

And here is how you print to a file:

CL-USER 2 > (defun save-it (object)
              (with-open-file (stream "my-object" :direction :output
                                      :if-exists :supersede)
                (prin1 object stream)))
SAVE-IT

CL-USER 3 > (defun restore-it ()
              (with-open-file (stream "my-object")
                (read stream)))
RESTORE-IT

CL-USER 4 > (save-it '(foo (bar "baz") 17 42.38))
(FOO (BAR "baz") 17 42.38)

CL-USER 5 > (restore-it)
(FOO (BAR "baz") 17 42.38)

This might suffice to get you started.  If you have large
structures, you might also want to try PPRINT instead of PRIN1,
so the content of the file looks a bit prettier...

Regards,
-- 
Nils Goesche
Ask not for whom the <CONTROL-G> tolls.

PGP key ID #xD26EF2A0