title: Haskell pipes: how to write bytestring to file
body: <p>I have the following code that should create a pipe Consumer for [Word8] and output it to a file.</p>
  
  <pre><code>import Pipes
  import qualified Pipes.Binary as PB
  import qualified Pipes.ByteString as PBS
  import qualified System.IO as SIO 
  
  save :: String -&gt; Consumer [Word8] IO ()
  save filename = do
    fh &lt;- lift $ SIO.openFile filename WriteMode
    _ &lt;- Pipes.for cat PB.encode &gt;-&gt; PBS.toHandle fh
    lift $ SIO.hClose fh
  </code></pre>
  
  <p>However, there is no output (although the file gets created). If I replace the "PBS.toHandle fh" with "PBS.stdout", I am getting the output (to stdout). </p>
  
  <p>I also tried to use SIO.withFile with the whole pipeline I create as an argument, like</p>
  
  <pre><code> SIO.withFile "output.bin" WriteMode $ \fh -&gt; 
     runEffect $ input-processing &gt;-&gt; Pipes.for cat PB.encode &gt;-&gt; PBS.toHandle fh
  </code></pre>
  
  <p>and it works. But in that case, I am clueless as to how to put SIO.withFile into the function with the above signature (returning the consumer only).</p>
  
  <p>Thank you for explanation about what I am doing wrong.</p>