I am trying to read from a file input stream. The file is of type .txt
and was produced in Windows. Each read-line looks some thing like:
"example^M"
That is, every line ends with ^M. I believe this is because of the
differences between the way Windows and Linux handle newlines in ASCII
text files. Anyhow, does anyone have any suggestions for dealing with
this? I will not be able to convert the file prior to opening it from
LISP. Is there any lisp functions that can do the conversion, or any
read or read-line functions that can get around this?
Thanks.
On Fri, 28 Mar 2008 18:26:33 -0700 (PDT), dstein64 <········@gmail.com> wrote:
> I am trying to read from a file input stream. The file is of type
> .txt and was produced in Windows. Each read-line looks some thing
> like: "example^M"
>
> That is, every line ends with ^M. I believe this is because of the
> differences between the way Windows and Linux handle newlines in
> ASCII text files. Anyhow, does anyone have any suggestions for
> dealing with this? I will not be able to convert the file prior to
> opening it from LISP. Is there any lisp functions that can do the
> conversion, or any read or read-line functions that can get around
> this?
That's what external formats are for (which you specify when you open
the file), but this is implementation-dependent. Here's for example
how LispWorks does it
http://www.lispworks.com/documentation/lw51/LWUG/html/lwuser-402.htm
and for your case the :EOL-STYLE part would be the most important one.
Most other Lisps have similar facilities.
A somewhat portable solution is this:
http://weitz.de/flexi-streams/
Edi.
--
European Common Lisp Meeting, Amsterdam, April 19/20, 2008
http://weitz.de/eclm2008/
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
Is there any way to check if the stream is at the end-of-line? Then if
it is not, I will run (read-line file), which will bring it to the
next line? This method will work fine if I can find some stream
function that checks if EOL is next without actually reading the next
token/line/...
dstein64 <········@gmail.com> writes:
> Is there any way to check if the stream is at the end-of-line? Then if
> it is not, I will run (read-line file), which will bring it to the
> next line? This method will work fine if I can find some stream
> function that checks if EOL is next without actually reading the next
> token/line/...
This won't help you solve your first problem.
(when (char/= #\newline (peek-char nil stream))
(read-line stream))
clisp converts automatically all type of line terminator to #\newline
when reading text files. Otherwise, you can also specifiy the
:line-terminator in the encoding (see ext:make-encoding) that you can
pass as :external-type.
But until you give us the precise (lisp-implementation-type) you are
using, we cannot give you the precise implementation manual reference
to search for the precise implementation specific :external-type to
use to read CRLF as #\newline.
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Klingon function calls do not have "parameters" -- they have
"arguments" and they ALWAYS WIN THEM."
> clisp converts automatically all type of line terminator to #\newline
> when reading text files. Otherwise, you can also specifiy the
> :line-terminator in the encoding (see ext:make-encoding) that you can
> pass as :external-type.
>
> But until you give us the precise (lisp-implementation-type) you are
> using, we cannot give you the precise implementation manual reference
> to search for the precise implementation specific :external-type to
> use to read CRLF as #\newline.
I am using SBCL, but I would also like it to work on Allegro. I can
use use #+allegro and #+sbcl though. Thanks.
dstein64 <········@gmail.com> writes:
>> clisp converts automatically all type of line terminator to #\newline
>> when reading text files. Otherwise, you can also specifiy the
>> :line-terminator in the encoding (see ext:make-encoding) that you can
>> pass as :external-type.
>>
>> But until you give us the precise (lisp-implementation-type) you are
>> using, we cannot give you the precise implementation manual reference
>> to search for the precise implementation specific :external-type to
>> use to read CRLF as #\newline.
>
>
> I am using SBCL, but I would also like it to work on Allegro. I can
> use use #+allegro and #+sbcl though. Thanks.
Start with
http://www.franz.com/support/documentation/8.1/doc/iacl.htm#external-formats-1
--
Duane Rettig ·····@franz.com Franz Inc. http://www.franz.com/
555 12th St., Suite 1450 http://www.555citycenter.com/
Oakland, Ca. 94607 Phone: (510) 452-2000; Fax: (510) 452-0182
dstein64 wrote:
> I am trying to read from a file input stream. The file is of type .txt
> and was produced in Windows. Each read-line looks some thing like:
> "example^M"
>
> That is, every line ends with ^M. I believe this is because of the
> differences between the way Windows and Linux handle newlines in ASCII
> text files. Anyhow, does anyone have any suggestions for dealing with
> this? I will not be able to convert the file prior to opening it from
> LISP. Is there any lisp functions that can do the conversion, or any
> read or read-line functions that can get around this?
>
> Thanks
Check each line. If the last character is a carriage-return,
remove it..