From: Giannandrea Castaldi
Subject: newbie question: how to load a binary file in an unsigned-byte array
Date:
Message-ID: <bivkjl$osj$1@lacerta.tiscalinet.it>
I'm trying to load a binary file into an unsigned-byte array. I've
written the following function but it doesn't work. Can anyone help me?
(defun load-output-bin-file (filename)
(let ((bin-file (make-pathname :name filename
:directory '(:relative "output")))
(data nil))
(with-open-file (str bin-file :direction :input
:element-type 'unsigned-byte)
(setq data (make-array (file-length str) :element-type
'unsigned-byte))
(do ((i (read-byte str nil -1)
(read-byte str nil -1))
(pos 0 (+ pos 1)))
((minusp i))
(setf (aref data pos) i)))))
Thanks a lot.
Giannandrea
From: Espen Vestre
Subject: Re: newbie question: how to load a binary file in an unsigned-byte array
Date:
Message-ID: <kwk78s8tyu.fsf@merced.netfonds.no>
Giannandrea Castaldi <········@tiscali.it> writes:
> I'm trying to load a binary file into an unsigned-byte array. I've
> written the following function but it doesn't work. Can anyone help me?
You forget to return the array as the return value of the function!
> ((minusp i))
here you could have used
((minusp i) data)
--
(espen)
From: Giannandrea Castaldi
Subject: Re: newbie question: how to load a binary file in an unsigned-byte array
Date:
Message-ID: <bivl1c$p1v$1@lacerta.tiscalinet.it>
Just a second after I've sent the email I've found the error: the
function didn't return the value of the variable data, the following
version seems to work well. Is there anyway a simpler way to load binary
file?
Giannandrea
(defun load-output-bin-file (filename)
(let ((bin-file (make-pathname :name filename
:directory '(:relative "output")))
(data nil))
(with-open-file (str bin-file :direction :input
:element-type 'unsigned-byte)
(setq data (make-array (file-length str) :element-type
'unsigned-byte))
(do ((i (read-byte str nil -1)
(read-byte str nil -1))
(pos 0 (+ pos 1)))
((minusp i))
(setf (aref data pos) i)))
data))
Giannandrea Castaldi wrote:
> I'm trying to load a binary file into an unsigned-byte array. I've
> written the following function but it doesn't work. Can anyone help me?
>
> (defun load-output-bin-file (filename)
> (let ((bin-file (make-pathname :name filename
> :directory '(:relative "output")))
> (data nil))
> (with-open-file (str bin-file :direction :input
> :element-type 'unsigned-byte)
> (setq data (make-array (file-length str) :element-type
> 'unsigned-byte))
> (do ((i (read-byte str nil -1)
> (read-byte str nil -1))
> (pos 0 (+ pos 1)))
> ((minusp i))
> (setf (aref data pos) i)))))
>
> Thanks a lot.
>
> Giannandrea
>
From: Espen Vestre
Subject: Re: newbie question: how to load a binary file in an unsigned-byte array
Date:
Message-ID: <kwfzjg8tnu.fsf@merced.netfonds.no>
Giannandrea Castaldi <········@tiscali.it> writes:
> Just a second after I've sent the email I've found the error:
the usenet version of the "looking over the shoulder" phenomenon :-)
> function didn't return the value of the variable data, the following
> version seems to work well. Is there anyway a simpler way to load
> binary file?
Using read-sequence is much faster and simpler:
(defun load-output-bin-file (filename)
(let ((bin-file filename)
(data nil))
(with-open-file (str bin-file :direction :input
:element-type 'unsigned-byte)
(setq data (make-array (file-length str) :element-type
'unsigned-byte))
(read-sequence data str)
data)))
(style hint: remove data from the first let, and replace setq of data
with a second let)
--
(espen)
From: Nils Goesche
Subject: Re: newbie question: how to load a binary file in an unsigned-byte array
Date:
Message-ID: <lybru4pojk.fsf@cartan.de>
Giannandrea Castaldi <········@tiscali.it> writes:
> I'm trying to load a binary file into an unsigned-byte array. I've
> written the following function but it doesn't work. Can anyone help
> me?
>
> (defun load-output-bin-file (filename)
> (let ((bin-file (make-pathname :name filename
> :directory '(:relative "output")))
> (data nil))
> (with-open-file (str bin-file :direction :input
> :element-type 'unsigned-byte)
> (setq data (make-array (file-length str) :element-type
> 'unsigned-byte))
> (do ((i (read-byte str nil -1)
> (read-byte str nil -1))
> (pos 0 (+ pos 1)))
> ((minusp i))
> (setf (aref data pos) i)))))
You never return the array DATA...
Then, are you sure you don't want the type (unsigned-byte 8) instead?
And lookup READ-SEQUENCE.
Regards,
--
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."
PGP key ID 0x0655CFA0