檢查檔案結束條件
對於大多數其他語言的標準 I / O 庫的做法有點違反直覺,Haskell 的 isEOF
不需要你在檢查 EOF 條件之前執行讀操作; 執行時會為你做。
import System.IO( isEOF )
eofTest::Int -> IO ()
eofTest line = do
end <- isEOF
if end then
putStrLn $ "End-of-file reached at line " ++ show line ++ "."
else do
getLine
eofTest $ line + 1
main::IO ()
main =
eofTest 1
輸入:
Line #1.
Line #2.
Line #3.
輸出:
End-of-file reached at line 4.