This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

With-open-file macro problem

7 posts · 8508 views

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.
(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

What's the error message?

Re: With-open-file macro problem

It not return error . I just did
(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

If I type what you've posted then I get an error:
? (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

Thank you my friend,that work !! Thank you a lot !

Re: With-open-file macro problem

One more question,when i write :
(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

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.