From: Nonzero
Subject: Using a file not in the current directory?
Date: 
Message-ID: <376b0355.0211101501.7f196ea1@posting.google.com>
Hello,  I have a lisp program that I would like to open a file that is
not in the same directory as the file.

So right now my file command looks like this:

(with-open-file (stream "file.dat")

When I try to make it get the file elsewhere like so:

(with-open-file (stream "/somewhere/else/rhat.stk")

*** - OPEN: file #P"/ABC/somewhereelsefile.dat" does not exist

Where /ABC/ is the directory that contains the lisp program.

Any ideas?

Thank you.
-Colin

From: Gabe Garza
Subject: Re: Using a file not in the current directory?
Date: 
Message-ID: <87adkh6jaa.fsf@ix.netcom.com>
····@geneseo.edu (Nonzero) writes:

> Hello,  I have a lisp program that I would like to open a file that is
> not in the same directory as the file.
> 
> So right now my file command looks like this:
> 
> (with-open-file (stream "file.dat")
> 
> When I try to make it get the file elsewhere like so:
> 
> (with-open-file (stream "/somewhere/else/rhat.stk")
> 
> *** - OPEN: file #P"/ABC/somewhereelsefile.dat" does not exist
> 
> Where /ABC/ is the directory that contains the lisp program.
> 
> Any ideas?

Plenty. :) Unlike any other language I know of, Common Lisp
differentiates between a string that can be interpreted as a path and
an actual path object.  The #P before "/ABC/somewhereelsefile.dat"
means that it's the printed representation of a pathname object, not
just a string.  Check out section 19 of the HyperSpec ("Filenames") 
for a whole lot more info.

If it's a file external to your program that will always be in the 
same place, you probably want to do something like:

(with-open-file (stream (make-pathname 
                          :directory '(:absolute "somewhere" "else") 
                          :name "rhat" 
                          :type "stk"))
  ...)

Or, if you never ever ever have to worry about your program being ported
away from its current platform,

(with-open-file (stream #P"/somewhere/else/rhat.stk")
  ...)
                              

If "rhat.stk" is part of your program (e.g., a log or data file?)?
You probably want to define a "logical host" for your program and
access the file through it.  A logical host is, to a very rough
approximation, your very own little imaginary filesystem that you
define by mapping back to a real filesystem(s).  For example:

(setf (logical-pathname-translations "MY-PROGRAM")
  '(("SRC;**;*.*.*" "/usr/local/projects/my-program/**/")
    ("DATA;*.*.*"   "/var/db/my-programs-data/")))

Would map "MY-PROGRAM;SRC;frobs;master.lisp" to
"/usr/local/projects/my-program/frobs/master.lisp" and
"MY-PROGRAM;DATA;rhat.stk" to
"/var/db/my-programs-data/rhat.stk". (Note that the last "*" in the
filename wildcard is for the version--unless you're on VMS or Genera
it probably won't be meaningful. The ";" is the directory separator in
the "logical host" syntax, and the "**" is a directory wildcard.
Using logical hosts obviously makes relocating your program a great
deal easier!

This may seem confusing--the filename bit took me the longest to "get"
and start using out of just about anything in Common Lisp, I think
because I had taken hardcoded paths for granted.  If I've been unclear, 
please reply!

Gabe Garza
From: Marco Antoniotti
Subject: Re: Using a file not in the current directory?
Date: 
Message-ID: <y6cvg34dpx4.fsf@octagon.valis.nyu.edu>
····@geneseo.edu (Nonzero) writes:

> Hello,  I have a lisp program that I would like to open a file that is
> not in the same directory as the file.
> 
> So right now my file command looks like this:
> 
> (with-open-file (stream "file.dat")
> 
> When I try to make it get the file elsewhere like so:
> 
> (with-open-file (stream "/somewhere/else/rhat.stk")
> 
> *** - OPEN: file #P"/ABC/somewhereelsefile.dat" does not exist
> 
> Where /ABC/ is the directory that contains the lisp program.
> 
> Any ideas?

First of all, the above idiom is usabel, but it has an implicit
dependency on the implementation.

By using the string "/somewhere/else/rhat.stk" (btw. the ".stk" at the
end makes me suspicious :) ) you are deferring to the implementation
dependent function PARSE-NAMESTRING to transform the string into a
pathname.  Think of the Java java.io.File class.

The error that you get is somewhat suspicious because it does not look
like any implementation I know of.  Under UNIX PARSE-NAMESTRING of the
string

         "/somewhere/else/rhat.stk"

would return something like

        #p"/somewhere/else/rhat.stk"

on most CL implementations.  Which is a shorthand for something like

        #S(PATHNAME :host <impl-dependent>
                    :device <impl-dependent, probably NIL>
                    :directory (:absolute "somewhere" "else")
                    :name "rhat"
                    :type "stk"
                    :version :newest ; or NIL
                    )

(The above is #S notation for pathanmes)

What you report looks strange.  What Common Lisp implementation and OS
combination are you using?

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
715 Broadway 10th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.
From: Barry Margolin
Subject: Re: Using a file not in the current directory?
Date: 
Message-ID: <M3Rz9.9$KQ4.729@paloalto-snr1.gtei.net>
In article <····························@posting.google.com>,
Nonzero <····@geneseo.edu> wrote:
>Hello,  I have a lisp program that I would like to open a file that is
>not in the same directory as the file.
>
>So right now my file command looks like this:
>
>(with-open-file (stream "file.dat")
>
>When I try to make it get the file elsewhere like so:
>
>(with-open-file (stream "/somewhere/else/rhat.stk")
>
>*** - OPEN: file #P"/ABC/somewhereelsefile.dat" does not exist

It looks like your Lisp implementation is ignoring "/" characters in
strings.  Are you sure you didn't write:

(with-open-file (stream "\somewhere\else\rhat.stk")

Backslash is an escape prefix character in strings, so if you want it to be
treated literally you must escape it:

(with-open-file (stream "\\somewhere\\else\\rhat.stk")

If you really did use the forward slashes, then it seems that your
implementation uses them as escapes as well (I think Zetalisp was like
this), and you'll need to prefix them with "\" to get them taken literally.

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.