Hi,<br>I don`t know if this is the right group to ask, but I would appreciate some help with the following.<br><br>The aim is to read a binary file from lisp.<br><br>The file contains a set of records, every record consists of the following data:<br>
(cffi:defcstruct ppp<br>
(nr :unsigned-int)<br>
(name :char :count 32) ;; this is the problematic line 1<br>
(alias :char :count 12) ;; this is the problematic line 2<br>
(m :double)<br>
(t :double)<br>
(p :double)<br>
(v :double))<br>
<br>Now the problematic line 1 should read 32 characters from an ascii-encoded file.<br>For that, I use the (defun test .....) (see below). The test converts the cstruct with-foreign-slots through (cffi:foreign-string-to-lisp name). <br>
The output of (test) is:<br>(1 "ABC ABC ÙÎ÷Só<@fffffŽ`@"<br> "ABC ÙÎ÷Só<@fffffŽ`@" 28.9d0 132.4d0 3774.0d0 0.0d0)<br>where:<br>(length (second *))<br>
>> 60<br>(length (third **))<br>>> 28<br>whereas I would expect the output to be:<br>(1 "ABC "<br>
"ABC " 28.9d0 132.4d0 3774.0d0 0.0d0)<br><br>with length second = 32 and length third = 12.<br><br>Am I missing something?<br>I tried:<br>(cffi:foreign-string-to-lisp name 32)<br>(cffi:foreign-string-to-lisp name 12)<br>
<br>this works, the correct output is presented. But how can I get the length of the char in the cstruct PPP?<br><br>Thank you very much,<br>best regards,<br>mosi<br><br>;;---- TEST.lisp ------<br>(in-package :cl-user)<br>
(eval-when (:compile-toplevel :load-toplevel) (require :cffi))<br><br>(defpackage :test (:use :cl))<br><br>(in-package :test)<br><br>(cffi:defcfun "fopen" :pointer (path :string) (mode :string))<br>
<br>(cffi:defcfun "fread" :unsigned-long<br> (ptr :pointer)<br> (size :unsigned-long)<br> (nmemb :unsigned-long)<br> (stream :pointer))<br><br>(cffi:defcfun "fclose" :int (fp :pointer))<br><br>(cffi:defcstruct ppp<br>
(nr :unsigned-int)<br> (name :char :count 32)<br> (alias :char :count 12)<br> (mw :double)<br> (tc :double)<br> (pc :double)<br> (vc :double))<br><br>(defparameter *file-path* "C://the-file.bin") ;;<br>
<br>(defun test ())<br>(let (file)<br> (unwind-protect<br> (progn<br> (setf file (fopen *file-path* "rb"))<br> (cffi:with-foreign-object (struct 'ppp)<br> (fread struct (cffi:foreign-type-size 'ppp) 1 file)<br>
(cffi:with-foreign-slots ((nr name alias m t p v)<br> struct<br> ppp)<br> (list nr<br> (cffi:foreign-string-to-lisp name)<br> (cffi:foreign-string-to-lisp alias)<br>
mw<br> tc<br> pc<br> vc))))<br> (when file (fclose file))))<br><br>;; run this by (test)<br>