problems with working with Handles
Niels Reyngoud
nreijngo@cs.uu.nl
Thu, 12 Jun 2003 14:05:40 +0200
Hello,
We're two students from the department of computer science at the
University of Utrecht (the Netherlands), and we're havind some severe
difficulties in working
with file handles in Haskell. Consider for example the following program:
main = do --let inputfile = "input.txt"
let inputtext = "testit"
let outputfile = "output.txt"
writeFile outputfile ""
handle2 <- openFileEx outputfile (BinaryMode WriteMode)
hPutStr handle2 (inputtext ++ " extra")
handle3 <- openFileEx outputfile (BinaryMode ReadMode)
inputtext2 <- hGetContents handle3
handle4 <- openFileEx outputfile (BinaryMode WriteMode)
hPutStr handle4 (inputtext ++ " extra2")
The text which should be in the outputfile is "testit extra extra2",
instead "testit extra2" is written in.
Another strange problem is with the following program:
-------------
main = do putStr "Give inputfile for use of Super encoding\n"
inputfile <- getLine
handle <- openFileEx inputfile (BinaryMode ReadMode)
text <- hGetContents handle
putStr "Give outputfile for use of Super encoding\n"
outputfile <- getLine
encode text outputfile
encode :: String -> String -> IO()
encode text outputfile = do outputhandle <- openFileEx outputfile
(BinaryMode WriteMode)
bwt (runlengthEncode text) outputhandle
bwthandle <- openFileEx outputfile (BinaryMode ReadMode)
bwttext <- hGetContents bwthandle
tracer' bwttext
mtfhandle <- openFileEx outputfile (BinaryMode WriteMode)
hPutStr mtfhandle (arithmeticEncode (runlengthEncode
(movetofront bwttext)))
tracer' :: String -> IO()
tracer' x = writeFile "temp.txt" x
-------------
This only seems to work when we're calling 'tracer' ', so if we're
actually doing somewhing with "bwttext" before using it...
Also, we're wondering when you can close a handle. Personally we thought
you could close it if you are "done" with it, yet if you've captured the
contents of a file like this:
handle <- openFileEx inputfile (BinaryMode ReadMe)
text <- hGetContents handle
hClose handle
putStr text
no text appears on the screen; "text" seems empty.
We're testing on a Windows machine by the way.
Regards,
Niels Reyngoud and Richard Nieuwenhuis