Haskell pipes: how to write bytestring to file

321 Views Asked by At

I have the following code that should create a pipe Consumer for [Word8] and output it to a file.

import Pipes
import qualified Pipes.Binary as PB
import qualified Pipes.ByteString as PBS
import qualified System.IO as SIO 

save :: String -> Consumer [Word8] IO ()
save filename = do
  fh <- lift $ SIO.openFile filename WriteMode
  _ <- Pipes.for cat PB.encode >-> PBS.toHandle fh
  lift $ SIO.hClose fh

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).

I also tried to use SIO.withFile with the whole pipeline I create as an argument, like

 SIO.withFile "output.bin" WriteMode $ \fh -> 
   runEffect $ input-processing >-> Pipes.for cat PB.encode >-> PBS.toHandle fh

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).

Thank you for explanation about what I am doing wrong.

0

There are 0 best solutions below