[Haskell-cafe] busting up a structured binary file

Bulat Ziganshin bulatz at HotPOP.com
Thu Dec 8 17:28:05 EST 2005


Hello Brian,

Thursday, December 08, 2005, 10:54:29 PM, you wrote:

BM> I need to extract text from a structed binary file.  Its a local
BM> database for a commercial app of proprietary structure, though the
BM> structure has been determined.  So I need to move along a number of
BM> bytes, take a few chunks of so many more bytes and grab the string and
BM> move on to the next record, dumping the contents - ultimately dumping

compile with "ghc --make a.hs"

import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.Storable
import System.IO

main = do
  h <- openBinaryFile "database" ReadMode
  hSeek h AbsoluteSeek 137
  buf <- mallocBytes 10
  hGetBuf h buf 10
  str <- peekCStringLen (buf,10)
  print str
  hClose h


-- 
Best regards,
 Bulat                            mailto:bulatz at HotPOP.com





More information about the Haskell-Cafe mailing list