[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


According to CLtL2, FILE-POSITION can be used with binary streams
(eg, streams with :element-type set to signed-byte or unsigned-byte).
When a file position is specified (eg, (file-position stream n))
the file position is to be set to the nth byte in the file.  This
is not correctly working in MCL 2.01b.  The following code demonstrates
this.  I show three functions, CT-FILE, a function to create a 
binary stream with :element-type of '(unsigned-byte 16);
TEST, which randomly accesses the file at a given file position;
and TEST-2, which reads out the numbers stored at the first n
bytes.  The calls to FILE-POSITION in both TEST and TEST-2 
fail to act according to the specification.  An example run follows.


(defun ct-file (f-name n-bytes)
  (with-open-file (ofile f-name
                  :direction :output
                  :if-exists :supersede
                  :if-does-not-exist :create
                :element-type '(unsigned-byte 16))
    (format t "~%Creating index file . . ")
    (dotimes (n n-bytes)
      (write-byte n ofile)))

(defun test (f-name n)
  (with-open-file (ifile f-name :direction :input
			 :element-type '(unsigned-byte 16))
    (file-position ifile n)
    (format t "~%Pos: ~S Val: ~S" (file-position ifile) (read-byte ifile)))

(defun test-2 (f-name n)
  (with-open-file (ifile f-name :direction :input
                  :element-type '(unsigned-byte 16))
    (dotimes (i n)
      (format t "~%Pos: ~S Val: ~S" (file-position ifile) (read-byte ifile))))

;;; the following is a test run ...

? (ct-file "testing" 100)

Creating index file . . 
? (test "testing" 10)

Pos: 10 Val: 5   ;;; VAL SHOULD BE 10 !

? (test-2 "testing" 10)

Pos: 0 Val: 0
Pos: 2 Val: 1   ;; POS SHOULD BE 1 !
Pos: 4 Val: 2   ;; POS SHOULD BE 2 ! etc.
Pos: 6 Val: 3
Pos: 8 Val: 4
Pos: 10 Val: 5
Pos: 12 Val: 6
Pos: 14 Val: 7
Pos: 16 Val: 8
Pos: 18 Val: 9