-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileRead.hs
More file actions
41 lines (35 loc) · 1.1 KB
/
FileRead.hs
File metadata and controls
41 lines (35 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Main
where
import IO
main = do
hSetBuffering stdin LineBuffering
doLoop
doLoop = do
putStrLn "Enter a command rFN or wFN or q to quit:"
command <- getLine
case command of
'q':_ -> return ()
'r':filename -> do putStrLn ("Reading " ++ filename)
doRead filename
doLoop
'w':filename -> do putStrLn ("Writing " ++ filename)
doWrite filename
doLoop
_ -> doLoop
doRead filename =
bracket (openFile filename ReadMode) hClose
(\h -> do contents <- hGetContents h
putStrLn "The first 100 chars:"
putStrLn (take 100 contents))
{-
doRead filename = do
contents <- readFile filename
putStrLn "The first 100 chars:"
putStrLn (take 100 contents)
-}
doWrite filename = do
putStrLn "Enter text to go into the file:"
contents <- getLine
bracket (openFile filename WriteMode) hClose
(\h -> hPutStrLn h contents)
-- vim: ts=4 sw=4 et: