title: How to write a 2D byte array to a binary file in Common Lisp?
body: <p>I guess this is an easy question for someone with Common Lisp experience. Not so much for me, who just started out with LISP.</p>
  
  <p>As you see in the next snippet below, I create a 800 by 600 array of type <code>UNSIGNED BYTE</code>.</p>
  
  <pre class="lang-lisp prettyprint-override"><code>(defun test-binary-save ()
      (let*
          ((width 800)
           (height 600)
           (arr (make-array (list width height) 
                  :element-type '(mod 256)
                  :initial-element 0)))
          (utilities::save-array-as-pgm "test.pgm" arr)))
  </code></pre>
  
  <p>And the function in my utilities package is supposed to write that in <code>P5 PGM</code> format do disk.</p>
  
  <pre class="lang-lisp prettyprint-override"><code>(defun save-array-as-pgm (filename buffer)
      "Writes a byte array as a PGM file (P5) to a file."
      (with-open-file 
          (stream filename 
              :element-type '(unsigned-byte 8)
              :direction :output 
              :if-does-not-exist :create
              :if-exists :supersede)
          (let* 
              ((dimensions (array-dimensions buffer))
               (width (first dimensions))
               (height (second dimensions))
               (header (format nil "P5~A~D ~D~A255~A" 
                  #\newline
                  width height #\newline
                  #\newline)))
  
              (loop 
                  :for char :across header 
                  :do (write-byte (char-code char) stream))
              ;(write-sequence buffer stream) &lt;&lt;-- DOES NOT WORK - is not of type SEQUENCE
          ))
      filename)
  
  </code></pre>
  
  <p>The equivalent (and working) C-function which does the same thing looks like this.</p>
  
  <pre class="lang-c prettyprint-override"><code>static
  int
  save_pgm
  ( const char* filename
  , size_t width
  , size_t height
  , const uint8_t* pixels
  )
  {
      if(NULL == filename)
          return 0;
      if(NULL == pixels)
          return 0;
  
      FILE *out = fopen(filename, "wb");
      if(NULL != out)
      {
          fprintf(out, "P5\n%zu %zu\n255\n", width, height);
          size_t nbytes = width * height;
          fwrite(pixels,1,nbytes,out);
          fclose(out);
          return 1;
      }
  
      return 0;
  }
  
  </code></pre>
  
  <p>Who can tell me how to fix my <code>save-array-as-pgm</code> function, preferably with writing the array in one go, instead of using a loop and <code>(write-byte (aref buffer y x) stream)</code>?</p>
  
  <p>Before I decided to ask this question here, I googled a lot and only found references to some packages which do fancy binary stuff - but this is a simple case and I look for a simple solution.</p>