有些身体可以帮助我什么是我的程序错了。当我试图运行下面的程序,我收到以下错误消息:Haskell:hSetFileSize:无效的参数(无效的参数)
hSetFileSize
:无效的参数(无效参数)
import System.IO
main = do
putStrLn "Enter file name (Including full path) to read"
fileName <- getLine
handle <- openFile fileName ReadMode
sizeBeforeTrunc <- hFileSize handle
content <- readFile fileName
putStrLn $ "Size of the file Before truncation is " ++ (show sizeBeforeTrunc) ++ " bytes"
putStrLn $ "Content of the file is " ++ content
putStrLn "**************************************"
let n = sizeBeforeTrunc `div` 2
putStrLn $ "Truncating file to " ++ (show n) ++ " bytes"
info1 <- hSetFileSize handle (toInteger 10)
putStrLn $ show info1
sizeAfterTrunc <- hFileSize handle
putStrLn $ "Size of the file After truncation is " ++ (show sizeAfterTrunc) ++ " bytes"
putStrLn $ "Content of the file is " ++ content
hClose handle
附加备注:使用'中openFile文件名WriteMode'会导致'内容的错误< - READFILE fileName',因为GHC的运行时只允许要么多个读者或一个作家。打开文件两次(一次获取大小和阅读内容;然后截断)似乎更合理。 – Zeta
@Zeta:好点,我已经更新了答案,以显示一个可能的解决方案,首先执行“ReadMode”操作,然后执行“WriteMode”操作。 – Cactus
另外,我宁愿使用一些定制的严格的IO函数(如[这些](https://hackage.haskell.org/package/strict/docs/System-IO-Strict.html))来读取文件之前截断;标准的IO函数不幸的是很懒惰。 – Cactus