# Newbie question on an I/O loop

**URL:** <https://rubytalk.org/t/newbie-question-on-an-i-o-loop/29007>\
**Category:** ruby-talk\
**Created:** [16 July 2006 08:25 UTC](https://rubytalk.org/t/newbie-question-on-an-i-o-loop/29007 "2006-07-16T08:25:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay\_Daniels](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@Jay\_Daniels](https://rubytalk.org/u/Jay_Daniels)\
**Post date:** [16 July 2006 08:25 UTC](https://rubytalk.org/t/newbie-question-on-an-i-o-loop/29007/1 "2006-07-16T08:25:43Z")

</div>

I got a question that's puzzling me. Am new to Ruby but programming for  
decades. Here's the problem. This apparently simple loop reads 396 bytes  
of a file and no more. The actual file is 1.5KB and I can display it  
easily in a hex dumper. The bytes Ruby has read are all correct. It just  
seems to stop after 396 bytes. Any idea what's wrong or where I should  
be looking?

Many thanks in advance,

JK Daniels

> > > code \>\>\>

if ARGV[0] == nil  
&nbsp;&nbsp;&nbsp;&nbsp;puts( "\n" + $help )  
else  
&nbsp;&nbsp;&nbsp;&nbsp;i = 0  
&nbsp;&nbsp;&nbsp;&nbsp;tfm = File.open( ARGV[0] )  
&nbsp;&nbsp;&nbsp;&nbsp;tfm.each\_byte{ |c| i = i + 1; printf( "%d: %X\n", i, c ) }  
&nbsp;&nbsp;&nbsp;&nbsp;if tfm.eof  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "at eof" else puts "not at eof"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

\<\<\< end code \<\<\<

This outputs the first 396 bytes and then state it's at EOF. If it makes  
any difference, the last three bytes it reads are (all hex): C0 A4 00  
and the next three (unread) bytes in the file are: 1A C0 B5

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [16 July 2006 10:14 UTC](https://rubytalk.org/t/newbie-question-on-an-i-o-loop/29007/2 "2006-07-16T10:14:48Z")

</div>

> I got a question that's puzzling me. Am new to Ruby but programming for  
> decades. Here's the problem.

Welcome to the red world

This apparently simple loop reads 396 bytes

> of a file and no more. The actual file is 1.5KB and I can display it  
> easily in a hex dumper. The bytes Ruby has read are all correct. It just  
> seems to stop after 396 bytes. Any idea what's wrong or where I should  
> be looking?
> 
> Many thanks in advance,
> 
> JK Daniels
> 
> \>\>\> code \>\>\>
> 
> if ARGV[0] == nil  
> &nbsp;&nbsp;&nbsp;&nbsp;puts( "\n" + $help )  
> else  
> &nbsp;&nbsp;&nbsp;&nbsp;i = 0  
> &nbsp;&nbsp;&nbsp;&nbsp;tfm = File.open( ARGV[0] )  
> &nbsp;&nbsp;&nbsp;&nbsp;tfm.each\_byte{ |c| i = i + 1; printf( "%d: %X\n", i, c ) }  
> &nbsp;&nbsp;&nbsp;&nbsp;if tfm.eof  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "at eof" else puts "not at eof"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> \<\<\< end code \<\<\<
> 
> This outputs the first 396 bytes and then state it's at EOF. If it makes  
> any difference, the last three bytes it reads are (all hex): C0 A4 00  
> and the next three (unread) bytes in the file are: 1A C0 B5

Hmm you are not reading a binary file under windows by any chance?  
In that case File.open needs a mode "rb".  
Anyway the ruby way to do what you want is more like this

File.open ARGV.first, "rb" do # I would put paranthesis, but just to  
demonstrate the  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# various possibilities  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>file\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.each\_byte {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>c\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i += 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "%d:%X" % [i, c]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
end  
puts "at eof ;)"

Cheers  
Robert

> **···**
>
> On 7/16/06, Jay Daniels \<jkdaniels@rock.com\> wrote:
> 
> --
> 
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Steven\_R](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@Steven\_R](https://rubytalk.org/u/Steven_R)\
**Post date:** [16 July 2006 13:25 UTC](https://rubytalk.org/t/newbie-question-on-an-i-o-loop/29007/3 "2006-07-16T13:25:06Z")

</div>

Jay Daniels wrote:

> I got a question that's puzzling me. Am new to Ruby but programming for decades. Here's the problem. This apparently simple loop reads 396 bytes of a file and no more. The actual file is 1.5KB and I can display it easily in a hex dumper. The bytes Ruby has read are all correct. It just seems to stop after 396 bytes. Any idea what's wrong or where I should be looking?
> 
> Many thanks in advance,
> 
> JK Daniels
> 
> > > > code \>\>\>
> 
> if ARGV[0] == nil  
> &nbsp;&nbsp;&nbsp;&nbsp;puts( "\n" + $help )  
> else  
> &nbsp;&nbsp;&nbsp;&nbsp;i = 0  
> &nbsp;&nbsp;&nbsp;&nbsp;tfm = File.open( ARGV[0] )  
> &nbsp;&nbsp;&nbsp;&nbsp;tfm.each\_byte{ |c| i = i + 1; printf( "%d: %X\n", i, c ) }  
> &nbsp;&nbsp;&nbsp;&nbsp;if tfm.eof  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "at eof" else puts "not at eof"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> \<\<\< end code \<\<\<
> 
> This outputs the first 396 bytes and then state it's at EOF. If it makes any difference, the last three bytes it reads are (all hex): C0 A4 00 and the next three (unread) bytes in the file are: 1A C0 B5

I tested this on a file that was 29,184 bytes - it worked fine.

Could it be something in your data or environment?

Sorry I couldn't be of more help.

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [16 July 2006 13:48 UTC](https://rubytalk.org/t/newbie-question-on-an-i-o-loop/29007/4 "2006-07-16T13:48:29Z")

</div>

> and the next three (unread) bytes in the file are: 1A C0 B5

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^  
moulon% /usr/bin/ruby -e 'p "%c" % 0x1A'  
"\032"  
moulon%

it's Control-Z, use "rb" to open the file

p.s.:

moulon% ruby -e 'p "%c" % 0x1A'  
-e:1: [BUG] Segmentation fault  
ruby 1.9.0 (2006-07-14) [i686-linux]

Aborted  
moulon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Jay\_Daniels](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@Jay\_Daniels](https://rubytalk.org/u/Jay_Daniels)\
**Post date:** [17 July 2006 06:31 UTC](https://rubytalk.org/t/newbie-question-on-an-i-o-loop/29007/5 "2006-07-17T06:31:28Z")

</div>

Thank you Guy, Steven, and Robert for solving the problem. I appreciate  
it. Never occurred to me it could be a control-z problem. Robert, thanks  
especially for showing the same code using typical Ruby metaphors. This  
also helps a lot.

J K Daniels

> > > re \>\>\>

ts wrote:

> **···**
>
> > \> and the next three (unread) bytes in the file are: 1A C0 B5  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^  
> > moulon% /usr/bin/ruby -e 'p "%c" % 0x1A'  
> > "\032"  
> > moulon%
> > 
> > it's Control-Z, use "rb" to open the file
> > 
> > p.s.:
> > 
> > moulon% ruby -e 'p "%c" % 0x1A'  
> > -e:1: [BUG] Segmentation fault  
> > ruby 1.9.0 (2006-07-14) [i686-linux]
> > 
> > Aborted  
> > moulon%
> > 
> > Guy Decoux
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
