# Weird error while reading binary files

**URL:** <https://www.ruby-forum.com/t/weird-error-while-reading-binary-files/80203>\
**Category:** Ruby\
**Created:** [December 22, 2006, 3:24am UTC](https://www.ruby-forum.com/t/weird-error-while-reading-binary-files/80203 "2006-12-22T03:24:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kishi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@kishi](https://www.ruby-forum.com/u/kishi)\
**Post date:** [December 22, 2006, 3:24am UTC](https://www.ruby-forum.com/t/weird-error-while-reading-binary-files/80203/1 "2006-12-22T03:24:38Z")

</div>

Hello everyone!

I just started messing around with Ruby and so far it’s been great.  
Somehow I’ve stepped into some weird error I can’t understand nor solve  
it… I’ve been trying to google about it but with no results…

Here’s what’s going on:  
I’ve got a file structure which is like:  
4 bytes - Number of entries  
For number of entries : 300 bytes of information… several integers,  
longs, floats, strings…

If I do:

theFile = File.open(‘FilePath’)

infoArray = Array.new

infoEntries = theFile.read(4).unpack(‘V’)[0]  
infoEntries.times do |i|  
infoArray[i] =  
theFile.read(300).unpack(‘VA4V2FS4V12A24A8A32A32A32A32A32V8’)  
end

It will go fine until infoArray[33]. After that it won’t work at all!  
Let me make myself clear:

irb(main):002:0\> theFile = File.open(‘FILEPATH’)  
=\> #\<File:FILEPATH\>  
irb(main):003:0\> theFile.pos = 10244  
=\> 10244  
irb(main):004:0\> theFile.read(4)  
=\> “H\001\000\000”  
irb(main):005:0\> theFile.pos  
=\> 14340  
irb(main):006:0\>

See how it jumped positions?! That’s odd… Is there a file-size limit or  
something like that? I’m under Windows, by the way…

---

<div class="post-metadata">

**Author:** ![kishi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@kishi](https://www.ruby-forum.com/u/kishi)\
**Post date:** [December 22, 2006, 3:30am UTC](https://www.ruby-forum.com/t/weird-error-while-reading-binary-files/80203/2 "2006-12-22T03:30:08Z")

</div>

I’ve tested it with and without unpack… Several files… And nothing.  
I haven’t tested the BinaryStructure thing class yet… But I don’t feel  
like using it… It shouldn’t be needed, at least.

Thanks!

[sorry about the double post, accidentaly posted it before finishing it]

---

<div class="post-metadata">

**Author:** ![kishi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@kishi](https://www.ruby-forum.com/u/kishi)\
**Post date:** [December 22, 2006, 4:26am UTC](https://www.ruby-forum.com/t/weird-error-while-reading-binary-files/80203/3 "2006-12-22T04:26:30Z")

</div>

Mauricio Kishi wrote:

> theFile = File.open(‘FilePath’)

On windows, you’ll want

theFile = File.open(‘FilePath’, “rb”)

to put the file in binary mode (doesn’t convert \r\n to \n).

---

<div class="post-metadata">

**Author:** ![kishi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@kishi](https://www.ruby-forum.com/u/kishi)\
**Post date:** [December 22, 2006, 4:31am UTC](https://www.ruby-forum.com/t/weird-error-while-reading-binary-files/80203/4 "2006-12-22T04:31:28Z")

</div>

Joel VanderWerf wrote:

> Mauricio Kishi wrote:
> 
> > theFile = File.open(‘FilePath’)
> 
> On windows, you’ll want
> 
> theFile = File.open(‘FilePath’, “rb”)
> 
> to put the file in binary mode (doesn’t convert \r\n to \n).

OMG! No! Somehow I’ve missed it on the newest .rb files… O\_O"  
Thanks a lot… And sorry for such a stupid thing.

---

<div class="post-metadata">

**Author:** ![kishi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@kishi](https://www.ruby-forum.com/u/kishi)\
**Post date:** [December 22, 2006, 4:28am UTC](https://www.ruby-forum.com/t/weird-error-while-reading-binary-files/80203/5 "2006-12-22T04:28:10Z")

</div>

Joel VanderWerf wrote:

> Mauricio Kishi wrote:
> 
> > theFile = File.open(‘FilePath’)
> 
> On windows, you’ll want
> 
> theFile = File.open(‘FilePath’, “rb”)
> 
> to put the file in binary mode (doesn’t convert \r\n to \n).

Ooops, that was a mistake while writing the post! I’m already opening  
the files on binary mode…
