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?
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
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.
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
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")
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
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.
(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")
"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.