summaryrefslogtreecommitdiff
path: root/libraries/base/tests/IO/openFile004.hs
blob: 2a6b8cc7cfce3848e162034aac295d5abdf0bc38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- !!! Open a non-existent file for writing

import Control.Monad
import Data.Char
import System.Directory
import System.IO

file = "openFile004.out"

main = do
  b <- doesFileExist file
  when b (removeFile file)

  h <- openFile file WriteMode
  hPutStr h "hello world\n"
  hClose h

  h <- openFile file ReadMode
  let loop = do
        b <- hIsEOF h
        if b then return ()
             else do c <- hGetChar h; putChar c; loop
  loop