problems with working with Handles

Hal Daume III hdaume@ISI.EDU
Thu, 12 Jun 2003 09:21:40 -0700 (PDT)


I believe this is related to the topic discussed a few weeks ago.  See

  http://haskell.org/pipermail/haskell/2003-May/011851.html

and responses.

At least, this is most likely the curprit of your second problem.

I'm not so sure about the first one.  I would expect "testit extratestit
extra2" to be the text in the output file.  I'm guessing this has
something to do with buffering.  When you open the files, try
'hSetBuffering h? NoBuffering' and see if that fixes it.

--
 Hal Daume III                                   | hdaume@isi.edu
 "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume

On Thu, 12 Jun 2003, Niels Reyngoud wrote:

> 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
> 
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>