title: How to read an array of binary data from a file in haskell?
body: <p>Trying to read a binary file of <code>Word8</code>, I have the following program:</p>
  
  <pre><code>import qualified Data.Binary as B
  type Chars = [B.Word8]
  
  printChars :: Chars -&gt; IO()
  printChars cs = mapM_ print cs
  
  main :: IO()
  main = do
    chars &lt;- B.decodeFile "chars"
    printChars chars
  </code></pre>
  
  <p>When I run it, I get an error:</p>
  
  <pre><code>$ ./test
  test: too few bytes. Failed reading at byte position 241
  </code></pre>
  
  <p>It seems <code>decodeFile</code> expects an infinite list. 
  How can I tell it to just read as many elements as possible?</p>
  
  <p>Edit: </p>
  
  <p>Here was the code I was looking for: (This works with any type, not just Word8.)</p>
  
  <pre><code>import Prelude hiding ( readFile )
  import Data.ByteString.Lazy ( readFile )
  import Data.Binary.Get ( isEmpty, runGet )
  import qualified Data.Binary as B
  
  type Chars = [B.Word8]
  
  printChars :: Chars -&gt; IO()
  printChars cs = mapM_ print cs
  
  -- see http://hackage.haskell.org/package/binary-0.7.1.0/docs/Data-Binary-Get.html
  -- function getTrades
  getChars = do
    e &lt;- isEmpty
    if e then return []
    else do
      c &lt;- B.get
      cs &lt;- getChars
      return (c:cs)
  
  main :: IO()
  main = do
    input &lt;- readFile "chars"
    printChars $ runGet getChars input
  </code></pre>