# Reading binary data stops unexpectedly

**URL:** <https://www.ruby-forum.com/t/reading-binary-data-stops-unexpectedly/76926>\
**Category:** Ruby\
**Created:** [March 5, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/reading-binary-data-stops-unexpectedly/76926 "2006-03-05T15:23:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gonchuki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@gonchuki](https://www.ruby-forum.com/u/gonchuki)\
**Post date:** [March 5, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/reading-binary-data-stops-unexpectedly/76926/1 "2006-03-05T15:23:27Z")

</div>

i’m trying to parse a binary file that simply contains Wav files one  
after the other packed with an arbitrary header that points where one  
file begins and the lenght of it.  
The case is that i want to ignore decomposing the header and just dump  
each file as i encounter the Wav headers, but Ruby stops reading data  
unexpectedly.

in a nutshell, my code goes like this:  
File.open(‘sound.dat’, ‘r’).each\_byte do |byte|  
#…stuff to process the data…  
end

The block exits at 15864 bytes without throwing any errors. I even tried  
reading the whole file to memory and it just loads those same 15864  
bytes (and it’s a 190MiB file!!!)

Is this a known Ruby bug? am i doing something wrong?

thanks in advance,  
/gonchuki

---

<div class="post-metadata">

**Author:** ![gonchuki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@gonchuki](https://www.ruby-forum.com/u/gonchuki)\
**Post date:** [March 5, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/reading-binary-data-stops-unexpectedly/76926/2 "2006-03-05T15:34:46Z")

</div>

Gonzalo R. wrote:

> in a nutshell, my code goes like this:  
> File.open(‘sound.dat’, ‘r’).each\_byte do |byte|  
> #…stuff to process the data…  
> end

open(‘sound.dat’, ‘rb’)

lopex

---

<div class="post-metadata">

**Author:** ![gonchuki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ccd318/32.png) [@gonchuki](https://www.ruby-forum.com/u/gonchuki)\
**Post date:** [March 5, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/reading-binary-data-stops-unexpectedly/76926/3 "2006-03-05T16:23:46Z")

</div>

Marcin MielÅ¼yÅ?ski wrote:

> Gonzalo R. wrote:
> 
> > in a nutshell, my code goes like this:  
> > File.open(‘sound.dat’, ‘r’).each\_byte do |byte|  
> > #…stuff to process the data…  
> > end
> 
> open(‘sound.dat’, ‘rb’)
> 
> lopex

nice!

i just couldn’t find it in the pickaxe 2 and forgot that ruby handles  
file open modes just like in C

thanks for your help.
