From: j.p.lewis

Subject: Re: binary IO

Date: 1996-7-26 18:42


I believe there is no portable way to do binary I/O of floats in CL, 
since binary streams take an integer type.  Can anyone correct me
on this?
  
A relatively easy alternative is to call the C stdio library routines;
I've used this approach successfully in acl4.2.

(in the example below you'll see that i finally decided that cl
packages are more trouble than they're worth... )
-----------------------------------------------------------------------
j.p.lewis         i n t e r v a l  r e s e a r c h         palo alto ca
<interval.com at zilla>        415 842 6113      http://www.idiom.com/~zilla


;;;; cstdio.lisp zilla jun/6 - call through c i/o (binary array i/o forexample)

(in-package :cl-user)
(defvar cstdio-loaded t)

#+allegro
(eval-when (compile load eval)
  (if (not (bound? 'acl-ff-list-entry-loaded))
      (loadpath "acl-ff-list-entry")))

#+allegro
(eval-when (compile load eval)
  (defvar cstdio-ff-loaded nil)
  (if (not cstdio-ff-loaded)
   (let ((nlink
    (ff:defforeign-list
     (list

       (ff-list-entry (cstdio-fopen "fopen")
		      (string string)
		      :integer)

       (ff-list-entry (cstdio-fclose "fclose")
		      (integer)
		      :void)

       (ff-list-entry (cstdio-fread "fread")
		      (simple-array fixnum fixnum integer)
		      :fixnum)

       (ff-list-entry (cstdio-fwrite "fwrite")
		      (simple-array fixnum fixnum fixnum)
		      :fixnum)

       ))))

    (if (not (= 0 nlink))
	(error "could not link ~a cstdio routines" nlink))
    );let
   );if
);eval-when

#+nil
(define (cstdio-test)
  (let ((f (cstdio-fopen "cstdio.test" "w"))
	(v (make-array 5 :element-type 'single-float))
    	(v2 (make-array 5 :element-type 'single-float)))
    (dotimes (i 5) (setf (aref v i) (* 1.111 i)))
    (cstdio-fwrite v 4 5 f)
    (cstdio-fclose f)
    (set! f (cstdio-fopen "cstdio.test" "r"))
    (cstdio-fread v2 4 5 f)
    (dotimes (i 5) (format t "~a " (aref v2 i))) (terpri)
    (cstdio-fclose f)
    ))