home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / lisp / 3223 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.5 KB  |  87 lines

  1. Path: sparky!uunet!mcsun!sunic!dkuug!cri.dk!csd!ok
  2. From: ok@csd.cri.dk (Ole Kristensen)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: a problem with reading a stream
  5. Message-ID: <ok.726739371@csd.cri.dk>
  6. Date: 11 Jan 93 08:02:51 GMT
  7. References: <1992Dec27.014528.17943@news.columbia.edu> <1993Jan6.013014.7927@pentagon-gw.army.mil>
  8. Sender: news@csd.cri.dk
  9. Organization: Computer Resources International A/S
  10. Lines: 75
  11.  
  12. grant@pentagon-gw.army.mil (Pete Grant) writes:
  13.  
  14. >In article <1992Dec27.014528.17943@news.columbia.edu> rs69@cunixb.cc.columbia.edu (Rong Shen) writes:
  15. >>Hi experts:
  16. >>
  17. >>    I am trying to write a function that asks a path to a file
  18. >>from the user and opens the file to read. There is no problem with
  19. >>opening the file, but when it tries to read, the interpreter
  20. >>complains.
  21. >>
  22. >>    Can anyone tell me why?
  23. >>
  24. >>    Thanks very much.
  25. >>
  26. >>--
  27. >>rs69@cunixb.cc.columbia.edu
  28. >>
  29. >>(defun read-path ()
  30. >>  (format t "Enter the path to the file to read, e.g. /tmp/foo/faq.txt")
  31. >>  (setf file-path-name (read-line))
  32. >>  (setf what-stream (open file-path-name :direction :input)) ; ok
  33. >>
  34. >>  (read what-stream nil 'This-is-the-end-of-the-world)) ; This line has error;
  35.  
  36. >1.  What is the error?  A trip into the debugger?  Returns 'THIS-IS-THE...?
  37.  
  38. >2.  What's in the file you're trying to read?
  39.  
  40. >3.  Suggestion:  Although (open ..) works, you should use WITH-OPEN-FILE
  41. >    e.g.,
  42. >    (with-open-file (input file-path-name)
  43. >      (loop as line = (read-line input nil nil)
  44. >            while line
  45. >            do
  46. >        (....)))
  47.  
  48. >  If you use (OPEN ..), then error off, the stream is left open and can
  49. >prevent you from further attempts to read the file until you explicitly
  50. >close it.  On some machine this can be less than straightforward.  On
  51. >Symbolics you can use (FS:CLOSE-ALL-FILES).
  52.  
  53. >Pete.
  54.  
  55.  
  56.  
  57. Hi Rong and Pete,
  58.  
  59. The code shown is sensible to typing errors. If the pathname
  60. does not exist, the listener will just hang. You can test it
  61. by issuing (read NIL NIL 'This-is-the-end). It can be avoided
  62. by something like below:
  63.  
  64. (defun read-file (pathname)
  65.   (let* ((in-stream (open pathname
  66.                           :direction :input
  67.                           :if-does-not-exist NIL))
  68.          (contents (if in-stream
  69.                        (read in-stream NIL NIL)
  70.                      NIL)))
  71.     (progn
  72.        (if in-stream
  73.            (close in-stream)
  74.          (pprint (list "read-file, no stream" pathname)))
  75.        contents)))
  76.  
  77.  
  78. The above function only returns the first S-expression in the file.
  79.  
  80.  
  81. Best regards,
  82.  
  83. Ole
  84.  
  85. A
  86. A
  87.