From: Jimmy
Subject: Reading to the end of a file
Date: 
Message-ID: <1180173292.715134.35910@o5g2000hsb.googlegroups.com>
Basically, I open a file like this:

(setf file (open "file.txt"))

and I then use (read) to extract text from it.  The thing is, when I'm
at the end of the file, (read) simply returns an error.  Is there any
way to check if I'm at the end of a file, so I can use (read) as many
times as I need to in order to read the complete file, without
worrying about errors?

From: Zach Beane
Subject: Re: Reading to the end of a file
Date: 
Message-ID: <m34plz4uzb.fsf@unnamed.xach.com>
Jimmy <··············@gmail.com> writes:

> Basically, I open a file like this:
> 
> (setf file (open "file.txt"))
> 
> and I then use (read) to extract text from it.  The thing is, when I'm
> at the end of the file, (read) simply returns an error.  Is there any
> way to check if I'm at the end of a file, so I can use (read) as many
> times as I need to in order to read the complete file, without
> worrying about errors?

See the documentation for READ:

   http://www.lispworks.com/documentation/HyperSpec/Body/f_rd_rd.htm

In particular, the EOF-ERROR-P parameter.

Zach
From: Pascal Bourguignon
Subject: Re: Reading to the end of a file
Date: 
Message-ID: <87bqg7hogy.fsf@thalassa.lan.informatimago.com>
Jimmy <··············@gmail.com> writes:

> Basically, I open a file like this:
>
> (setf file (open "file.txt"))
>
> and I then use (read) to extract text from it.  The thing is, when I'm
> at the end of the file, (read) simply returns an error.  Is there any
> way to check if I'm at the end of a file, so I can use (read) as many
> times as I need to in order to read the complete file, without
> worrying about errors?

Yes.  The way is to read CLHS.

      http://www.lispworks.com/documentation/HyperSpec/Body/f_rd_rd.htm

So you can learn that you can write

(loop
  :for item = (read file nil file)
  :until (eq item file) ; eof
  :do (process-item item))


Note that if you want to extract _text_, then you should rather use
READ-LINE; since lines are strings and cannot be NIL, you can write
the simplier form:

(loop
  :for line = (read-line file nil nil)
  :while line
  :do (process-line line))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
From: Ken Tilton
Subject: Re: Reading to the end of a file
Date: 
Message-ID: <c0X5i.1166$3N7.804@newsfe12.lga>
Pascal Bourguignon wrote:
> Jimmy <··············@gmail.com> writes:
> 
> 
>>Basically, I open a file like this:
>>
>>(setf file (open "file.txt"))
>>
>>and I then use (read) to extract text from it.  The thing is, when I'm
>>at the end of the file, (read) simply returns an error.  Is there any
>>way to check if I'm at the end of a file, so I can use (read) as many
>>times as I need to in order to read the complete file, without
>>worrying about errors?
> 
> 
> Yes.  The way is to read CLHS.
> 
>       http://www.lispworks.com/documentation/HyperSpec/Body/f_rd_rd.htm

Bad example. Nothing happens when you get to the end of that.

kenny
From: Edi Weitz
Subject: Re: Reading to the end of a file
Date: 
Message-ID: <ufy5jy8z1.fsf@agharta.de>
On Sat, 26 May 2007 09:56:24 -0400, Ken Tilton <···········@optonline.net> wrote:

>> Yes.  The way is to read CLHS.
>>       http://www.lispworks.com/documentation/HyperSpec/Body/f_rd_rd.htm
>
> Bad example. Nothing happens when you get to the end of that.

You should be enlightened at that point, shouldn't you?

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Ken Tilton
Subject: Re: Reading to the end of a file
Date: 
Message-ID: <n616i.1571$Nj2.276@newsfe12.lga>
Edi Weitz wrote:
> On Sat, 26 May 2007 09:56:24 -0400, Ken Tilton <···········@optonline.net> wrote:
> 
> 
>>>Yes.  The way is to read CLHS.
>>>      http://www.lispworks.com/documentation/HyperSpec/Body/f_rd_rd.htm
>>
>>Bad example. Nothing happens when you get to the end of that.
> 
> 
> You should be enlightened at that point, shouldn't you?
> 

You have not read my road to lisp? <sigh>

   http://wiki.alu.org/Kenny_Tilton's_Road_to_Lisp

kenny
From: Jimmy
Subject: Re: Reading to the end of a file
Date: 
Message-ID: <1180202784.053992.161700@u30g2000hsc.googlegroups.com>
On May 26, 5:24 am, Pascal Bourguignon <····@informatimago.com> wrote:
> Jimmy <··············@gmail.com> writes:
> > Basically, I open a file like this:
>
> > (setf file (open "file.txt"))
>
> > and I then use (read) to extract text from it.  The thing is, when I'm
> > at the end of the file, (read) simply returns an error.  Is there any
> > way to check if I'm at the end of a file, so I can use (read) as many
> > times as I need to in order to read the complete file, without
> > worrying about errors?
>
> Yes.  The way is to read CLHS.
>
>      http://www.lispworks.com/documentation/HyperSpec/Body/f_rd_rd.htm
>
> So you can learn that you can write
>
> (loop
>   :for item = (read file nil file)
>   :until (eq item file) ; eof
>   :do (process-item item))
>
> Note that if you want to extract _text_, then you should rather use
> READ-LINE; since lines are strings and cannot be NIL, you can write
> the simplier form:
>
> (loop
>   :for line = (read-line file nil nil)
>   :while line
>   :do (process-line line))
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/
>
> NOTE: The most fundamental particles in this product are held
> together by a "gluing" force about which little is currently known
> and whose adhesive power can therefore not be permanently
> guaranteed.

Thanks Pascal, that worked great.
From: Alex Mizrahi
Subject: Re: Reading to the end of a file
Date: 
Message-ID: <46580ee4$0$90274$14726298@news.sunsite.dk>
(message (Hello 'Jimmy)
(you :wrote  :on '(26 May 2007 02:54:52 -0700))
(

 J> (setf file (open "file.txt"))

 J> and I then use (read) to extract text from it.

if you want just to extact all text into a string:

(defun read-file-to-string (filename)
  (with-open-file (s filename)
    (let* ((str (make-string (file-length s)))
           (bytes-read (read-sequence str s)))
      (subseq str 0 bytes-read))))


)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"I am everything you want and I am everything you need") 
From: Richard M Kreuter
Subject: Re: Reading to the end of a file
Date: 
Message-ID: <877iqvjdn1.fsf@tan-ru.localdomain>
"Alex Mizrahi" <········@users.sourceforge.net> writes:

> (message (Hello 'Jimmy)
> (you :wrote  :on '(26 May 2007 02:54:52 -0700))
> (
>
>  J> (setf file (open "file.txt"))
>
>  J> and I then use (read) to extract text from it.
>
> if you want just to extact all text into a string:
>
> (defun read-file-to-string (filename)
>   (with-open-file (s filename)
>     (let* ((str (make-string (file-length s)))
>            (bytes-read (read-sequence str s)))
>       (subseq str 0 bytes-read))))

Note that in general the count returned by FILE-LENGTH might be
measured in units unrelated to the number of characters in the file
(probably octets), in which case the string may be much larger than
needed for the file.