From: "Ara.T.Howard" Date: 2003-12-05T12:32:06+09:00 Subject: Re: how to navigate in a file ? On Fri, 5 Dec 2003, Boris "BXS" Schulz wrote: > Date: Fri, 05 Dec 2003 01:58:22 +0100 > From: Boris "BXS" Schulz > Newsgroups: comp.lang.ruby > Subject: how to navigate in a file ? > > Hi, > > I would like to open a binary file and read its contents. > The opening is no problem, but if I wanted to jump to a specific byte, > how would I do that ? Also, how would I read this specific byte ? I have > some code reading the byte, but it quite ugly. Is there something like > DATA.readbyte(position) ? > > greetings, BXS ~/eg/ruby > cat byte.rb require 'mmap' # guy's package from the RAA - get it! binary = 'bin.dat' # create a binary file open(binary, 'wb') do |f| bytes = [1 << 2, 1 << 1].pack 'cc' f.write bytes end # memory map it (man mmap) mmap = Mmap.new binary, 'r' byte_a, byte_b = mmap[0], mmap[1] print byte_a print byte_b puts ~/eg/ruby > ruby byte.rb 42 in addition to being really easy to use, this package is _really_ fast. -a -- ATTN: please update your address books with address below! =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | STP :: http://www.ngdc.noaa.gov/stp/ | NGDC :: http://www.ngdc.noaa.gov/ | NESDIS :: http://www.nesdis.noaa.gov/ | NOAA :: http://www.noaa.gov/ | US DOC :: http://www.commerce.gov/ | | The difference between art and science is that science is what we | understand well enough to explain to a computer. | Art is everything else. | -- Donald Knuth, "Discover" | | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done' ===============================================================================