home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sunic!dkuug!cri.dk!csd!ok
- From: ok@csd.cri.dk (Ole Kristensen)
- Newsgroups: comp.lang.lisp
- Subject: Re: a problem with reading a stream
- Message-ID: <ok.726739371@csd.cri.dk>
- Date: 11 Jan 93 08:02:51 GMT
- References: <1992Dec27.014528.17943@news.columbia.edu> <1993Jan6.013014.7927@pentagon-gw.army.mil>
- Sender: news@csd.cri.dk
- Organization: Computer Resources International A/S
- Lines: 75
-
- grant@pentagon-gw.army.mil (Pete Grant) writes:
-
- >In article <1992Dec27.014528.17943@news.columbia.edu> rs69@cunixb.cc.columbia.edu (Rong Shen) writes:
- >>Hi experts:
- >>
- >> I am trying to write a function that asks a path to a file
- >>from the user and opens the file to read. There is no problem with
- >>opening the file, but when it tries to read, the interpreter
- >>complains.
- >>
- >> Can anyone tell me why?
- >>
- >> Thanks very much.
- >>
- >>--
- >>rs69@cunixb.cc.columbia.edu
- >>
- >>(defun read-path ()
- >> (format t "Enter the path to the file to read, e.g. /tmp/foo/faq.txt")
- >> (setf file-path-name (read-line))
- >> (setf what-stream (open file-path-name :direction :input)) ; ok
- >>
- >> (read what-stream nil 'This-is-the-end-of-the-world)) ; This line has error;
-
- >1. What is the error? A trip into the debugger? Returns 'THIS-IS-THE...?
-
- >2. What's in the file you're trying to read?
-
- >3. Suggestion: Although (open ..) works, you should use WITH-OPEN-FILE
- > e.g.,
- > (with-open-file (input file-path-name)
- > (loop as line = (read-line input nil nil)
- > while line
- > do
- > (....)))
-
- > If you use (OPEN ..), then error off, the stream is left open and can
- >prevent you from further attempts to read the file until you explicitly
- >close it. On some machine this can be less than straightforward. On
- >Symbolics you can use (FS:CLOSE-ALL-FILES).
-
- >Pete.
-
-
-
- Hi Rong and Pete,
-
- The code shown is sensible to typing errors. If the pathname
- does not exist, the listener will just hang. You can test it
- by issuing (read NIL NIL 'This-is-the-end). It can be avoided
- by something like below:
-
- (defun read-file (pathname)
- (let* ((in-stream (open pathname
- :direction :input
- :if-does-not-exist NIL))
- (contents (if in-stream
- (read in-stream NIL NIL)
- NIL)))
- (progn
- (if in-stream
- (close in-stream)
- (pprint (list "read-file, no stream" pathname)))
- contents)))
-
-
- The above function only returns the first S-expression in the file.
-
-
- Best regards,
-
- Ole
-
- A
- A
-