2013-05-16 15 views
2

我想獲得一個程序來讀取整個文件使用hFlush,以避免我遇到的問題與懶惰的IO有關。haskell hFlush不工作的方式我期待

readHandle <- openFile fileName ReadMode 
hSetBuffering readHandle $ BlockBuffering (Just 2048) 
fileText <- hGetContents readHandle 
hFlush readHandle 
hClose readHandle 

這只是給我的錯誤:hFlush:非法操作(關閉句柄)

能有人幫助我瞭解什麼是對

+3

見http://stackoverflow.com/questions/5373883/haskell-laziness-how-do-i-force-the-io-to-happen-sooner?rq=1 –

+3

還,'hFlush'刷新_output_緩衝區。首先在只讀句柄上使用它沒有任何意義。 – hammar

+1

我推薦使用'System.IO.Strict'中的'hGetContents',並避免使用懶惰IO。 –

回答

6

你的期望會是錯誤的。一般情況下,除了明確關閉句柄之外,在撥打hGetContents之後,您不應使用句柄。從鱈魚:

Computation hGetContents hdl returns the list of characters corresponding to the unread portion of the channel or file managed by hdl, which is put into an intermediate state, semi-closed. In this state, hdl is effectively closed, but items are read from hdl on demand and accumulated in a special list returned by hGetContents hdl .