Page 1 of 1

With-open-file macro problem

Posted: Sat Oct 24, 2015 10:17 am
by Domus123
Hello guys,i have this question . I have created a code to manipulate some list saved in a global variable . But now,i want that global to get the value of some data "deseases.dat" and then use this on the others functions that i have created.

Code: Select all

(defun get-data()  
   (with-open-file (stream "deseases.dat" )
     (setf (glob  (read stream)))))


This way i want to use my global variable "glob" that get the valor of the deseases.dat file and now i can manypulate that data using the glob,tem make another with-file-open to save this on deseases.dat . But,i kind of not understand how with-open-file works and why this function inst working .
Someone can give me a tipe of how this works?
And sorry for bad english. Thank you !

Re: With-open-file macro problem

Posted: Sat Oct 24, 2015 2:13 pm
by David Mullen
What's the error message?

Re: With-open-file macro problem

Posted: Sat Oct 24, 2015 6:10 pm
by Domus123
It not return error . I just did

Code: Select all

(defvar glob nil)
And then,after (get-data) setf dont add the data value to the global variable glob.When i call the get-data function it read the file but dont associate the file value to the variable

Re: With-open-file macro problem

Posted: Sat Oct 24, 2015 8:12 pm
by David Mullen
If I type what you've posted then I get an error:

Code: Select all

? (defvar glob nil)
GLOB
? (defun get-data()
   (with-open-file (stream "deseases.dat" )
     (setf (glob  (read stream)))))
> Error: Odd number of args to SETF : ((GLOB (READ STREAM))).
> While executing: (:INTERNAL CCL::NX1-COMPILE-LAMBDA), in process listener(1).
> Type :GO to continue, :POP to abort, :R for a list of available restarts.
> If continued: continue compilation ignoring this form
> Type :? for other options.
Meaning it should've been (setf glob (read stream)) without the extra parentheses. Setf takes a list of alternating places and values.

Re: With-open-file macro problem

Posted: Sun Oct 25, 2015 6:08 am
by Domus123
Thank you my friend,that work !! Thank you a lot !

Re: With-open-file macro problem

Posted: Sun Oct 25, 2015 6:41 am
by Domus123
One more question,when i write :

Code: Select all

(defun save-data()
   (with-open-file (stream "deseases.dat" :direction :output)
      (format stream "~S" glob)))
What i have to do to overwrite the deseases.dat file instead of creating a new one ?

Re: With-open-file macro problem

Posted: Sun Oct 25, 2015 1:12 pm
by David Mullen
What happens when you want to perform output to a file that already exists is controlled by the :if-exists option. The possibilities—per the standard—run from :new-version (for versioning file systems, e.g. Files-11) to :rename-and-delete and :append and then some.

Problem is, the standard is lax here—it just says to do something "reasonable" with the options, and that one might "deviate slightly from the semantics specified here without being disqualified for consideration as a conforming implementation." What's reasonable depends mainly on what's important to whoever authored your Common Lisp implementation.

Semantically, :if-exists :supersede is probably what you want. But I use Clozure CL, and I learned the hard way that they implemented :supersede in a way that doesn't always work on Windows. I mostly use ":overwrite" (which won't truncate the existing file) and :append. Other options—including :new-version, since hardly anyone has a versioning file system nowadays—will truncate the existing file.