title: Why does decodeFile throw "not enough bytes" error?
body: <p>Why does <code>decodeFile</code> from <code>Data.Binary</code> throw the error <code>*** Exception: demandInput: not enough bytes</code>, when using <code>decode</code> and <code>readFile</code> from <code>Data.ByteString.Lazy</code> works as expected?</p>
  
  <p>A minimal example</p>
  
  <pre><code>module Testing where
  
  import Data.Binary
  import qualified Data.Map.Strict as Map
  import qualified Data.ByteString.Lazy as B
  
  x :: Map.Map Char Int
  x = Map.fromList $ zip ['a'..'d'] [1,2,3,4]
  
  main :: IO (Map.Map Char Int)
  main = do
    encodeFile "testing" (encode x)
    r &lt;- decodeFile "testing"
    print $ r == x
    return r
  
  main2 :: IO (Map.Map Char Int)
  main2 = do
    B.writeFile "testing2" (encode x)
    r &lt;- fmap decode $ B.readFile "testing2"
    print $ r == x
    return r
  </code></pre>
  
  <p>I found this potentially relevant question: <a href="https://stackoverflow.com/questions/18522278/using-data-binary-decodefile-encountered-error-demandinput-not-enough-bytes">Using Data.Binary.decodeFile, encountered error &quot;demandInput: not enough bytes&quot;</a> but it does not really answer my question, which is why using <code>decodeFile</code> doesn't work but <code>readFile</code> does.</p>
  
  <p>The output I get:</p>
  
  <pre><code>[1 of 1] Compiling Testing          ( testing.hs, interpreted )                                                                                                         
  Ok, modules loaded: Testing.                                                                                                                                            
  *Testing&gt; main                                                                                                                                                          
  *** Exception: demandInput: not enough bytes              
  
  *Testing&gt; main2                                                                                                                                                         
  True                                                                                                                                                                    
  fromList [('a',1),('b',2),('c',3),('d',4)]   
  </code></pre>