
to Ketil : Tring openBinaryFile, I notice that I cannot make one usable buffer, just because I can not find one function to "malloc" a memory or just get one "change-able" buffer. :-$ to Marc: I can not locate which module including readBinaryFile. And I use hoogle search engine. Could you give me some more hints ? ------------------ L.Guo 2007-05-24

leaveye.guo:
to Ketil :
Tring openBinaryFile, I notice that I cannot make one usable buffer, just because I can not find one function to "malloc" a memory or just get one "change-able" buffer.
:-$
No 'malloc' here in Haskell land: that's done automatically. Recall that 'getContents' will read your opened file into a [Char]. (or use Data.ByteString to get a stream of Word8). What are you trying to do? -- Don

To read the handle openBinaryFile returns, both the hGetBuf and hGetBufNonBlocking needs one parameter _buf_ of type Ptr a. I can not get one data of that type. In the doc, there is only nullPtr, and also some type cast functions. I failed to find some other buffer-maker function. What should I do ? ------------------ L.Guo 2007-05-24 ------------------------------------------------------------- From: Donald Bruce Stewart At: 2007-05-24 17:03:55 Subject: Re: Re: [Haskell-cafe] (no subject) What are you trying to do? -- Don

leaveye.guo:
To read the handle openBinaryFile returns, both the hGetBuf and hGetBufNonBlocking needs one parameter _buf_ of type Ptr a. I can not get one data of that type.
In the doc, there is only nullPtr, and also some type cast functions. I failed to find some other buffer-maker function.
What should I do ?
I mean, what problem are you trying to solve? Ptrs aren't the usual way to manipulate files in Haskell. Here, for example, is a small program to print the first byte of a binary file: import System.IO import qualified Data.ByteString as B main = do h <- openBinaryFile "a.out" ReadMode s <- B.hGetContents h print (B.head s) When run: $ ./a.out 127 Note there's no mallocs or pointers involved. -- Don

Very thanks for your example, I have not notice that there is a group of hGetxxx functions in ByteString. In other words, I was using hGetxxx which implemented in IO module. So it always failed. ------------------ L.Guo 2007-05-24 ------------------------------------------------------------- From: Donald Bruce Stewart At: 2007-05-24 17:31:02 Subject: Re: Re: [Haskell-cafe] (no subject) I mean, what problem are you trying to solve? Ptrs aren't the usual way to manipulate files in Haskell. ... -- Don

On Thu, 2007-05-24 at 17:01 +0800, L.Guo wrote:
Tring openBinaryFile,
Well, did you get it to work?
I can not locate which module including readBinaryFile.
This is what I find in System.IO (ghci> :b System.IO): openBinaryFile :: FilePath -> IOMode -> IO Handle openBinaryTempFile :: FilePath -> String -> IO (FilePath, Handle) hSetBinaryMode :: Handle -> Bool -> IO () so you have the option of either using openBinaryFile or openFile and using hSetBinaryMode to true. I guess - I've never had to use them. I can't find a readBinaryFile either, but writing one might be a good excercise? Makes me wonder whether one should have binary be the default? I'm a stranger in Windows-land, but are there cases where you want reading of a file to be terminated on ^Z? Seems pretty awful to me. Concerning mutable buffers, it is of course possible, but hardly idiomatic Haskell. Why do you need mutability? -k

Hello, Ketil Malde wrote:
Makes me wonder whether one should have binary be the default? I'm a stranger in Windows-land, but are there cases where you want reading of a file to be terminated on ^Z? Seems pretty awful to me.
The ghc docs state about openBinaryFile:
Like openFile, but open the file in binary mode. On Windows, reading a file in text mode (which is the default) will translate CRLF to LF, and writing will translate LF to CRLF. [...] text mode treats control-Z as EOF
The CRLF-to-LF translation is the more important part. It allows '\n' to stand for the end of a line on windows, too, even if lines are terminated by two characters in windows text files. Tillmann
participants (4)
-
dons@cse.unsw.edu.au
-
Ketil Malde
-
L.Guo
-
Tillmann Rendel