[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Reading from binary input streams??
Bruce L. Lambert writes:
> but when I say
>
> (with-open-file (istream "filename" :direction :input :element-type
> '(signed-byte 4)) (read-byte istream))
>
> My system (CLISP) says "filename is not an integer file."
Well. When CLISP opens a file with element-type (signed-byte 4), it does
not know whether the number of items in the file is even or odd - because
the system's notion of file length is measured in bytes, so you can't
decide from the file length whether it contains 2n or 2n-1 elements.
To overcome this, CLISP expects 4-byte header in front of the data, giving
length (in items) of the file.
> What do I do to read I this binary data?
Add a 4 byte header to the file, and put in the number of items ...
[looking deep into the source] in little-endian format.
> "common lisp currently specifies only a very
> simple faciltiy for binary input [output]" known as read-byte or write-byte
> respectively.
Now there is also `read-sequence' in ANSI CL, called `read-byte-sequence'
in CLISP. As with `read-byte' the translation unit can be 4-bit numbers.
if that's the way you opened the stream.
Bruno Haible email: <haible@ilog.fr>
Software Engineer phone: +33-1-49083585