Haskell Language Checking for end-of-file conditions


Example

A bit counter-intuitive to the way most other languages' standard I/O libraries do it, Haskell's isEOF does not require you to perform a read operation before checking for an EOF condition; the runtime will do it for you.

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

Input:

Line #1.
Line #2.
Line #3.

Output:

End-of-file reached at line 4.