[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Reading binary files
From: |
Felix Winkelmann |
Subject: |
Re: [Chicken-users] Reading binary files |
Date: |
Mon, 23 Feb 2004 07:45:13 +0100 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040113 |
Magnus Therning wrote:
My first post :-)
Very good! I hope it isn't the last. ;-)
I need to read a file that has the following basic layout:
table-size - 32 bits
[ frequency - 32 bits
byte - 8 bits]+
message-size - 32 bits
message ...
Now, the problem I have is that I have never used scheme for programming
on this low level before -- I have never had to read a specific number
of bytes from a file and convert those bytes to an integer. I can't find
any information on how to do it, I guess SRFI-4 is related, but from the
webpage that the chicken manual refers to[1] I can't figure it out. I
also can't find any kind of read-byte, read-32bits, or similar function
in the manual.
You could simply use `read-char' and convert the character into a byte
with `char->integer'. If you are using Windows, you should also open
the file with `#:binary'
(open-input-file filename #:binary)
to avoid CRLF translation of line terminators.
`read-string' might also be helpful. So for example:
(declare (uses extras lolevel srfi-4)); for `read-string',
`string->byte-vector' and
`byte-vector->u8vector'
(define i (open-input-file "myfile" #:binary))
(define s (read-string 32 i)); read 32 bytes from file
(define v (string->byte-vector s)); gives you a byte-vector
(define u (byte-vector->u8vector v)); if you want a SRFI-4 bytevector instead
cheers,
felix