我需要讀取,修改並更新相同功能中的某些文件。理想的解決方案(見下文)無法工作,這是錯誤的。 「最差」解決方案有效。最短的代碼來讀取文件,然後更新它?
-- Ex. "Pseudocode" - Doesn't work.
ideal = let pathFile = "C:\\TEMP\\Foo.txt"
in readFile pathFile >>= writeFile pathFile . (++ "!")
-- It works.
worst = do
let pathFile = "C:\\TEMP\\Foo.txt"
h <- openFile pathFile ReadMode
cs <- hGetContents h
(temp_Foo,temp_handle) <- openTempFile pathFile
hPrint temp_handle $ cs ++ "!"
hClose temp_handle
removeFile pathFile
renameFile temp_Foo pathFile
我希望避免2010年「簡單而醜陋的解決方法」,由裏德·波頓建議:
doit file = do
contents <- readFile file
length contents `seq` (writeFile file $ process contents)
有沒有更好的解決辦法?
那麼,你可以有一個管道/管道解決方案。 :) – Sibi
@Sibi這些庫很大。我認爲這需要很長時間才能掌握。另外,我不認爲用大炮殺死蚊子。 –
@AlbertoCapitani你不需要大炮來殺死蚊子 - 但是你也不需要超級計算機(按70年代的標準)撥打電話。 – leftaroundabout