# Newbie question on an I/O loop

**URL:** <https://www.ruby-forum.com/t/newbie-question-on-an-i-o-loop/98580>\
**Category:** Ruby\
**Created:** [July 16, 2006, 10:25am UTC](https://www.ruby-forum.com/t/newbie-question-on-an-i-o-loop/98580 "2006-07-16T10:25:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e8c25b/32.png) [@bloc](https://www.ruby-forum.com/u/bloc)\
**Post date:** [July 16, 2006, 10:25am UTC](https://www.ruby-forum.com/t/newbie-question-on-an-i-o-loop/98580/1 "2006-07-16T10:25:26Z")

</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  
puts( “\n” + $help )  
else  
i = 0  
tfm = File.open( ARGV[0] )  
tfm.each\_byte{ |c| i = i + 1; printf( “%d: %X\n”, i, c ) }  
if tfm.eof  
puts “at eof” else puts “not at eof”  
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

---

<div class="post-metadata">

**Author:** ![bloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e8c25b/32.png) [@bloc](https://www.ruby-forum.com/u/bloc)\
**Post date:** [July 16, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/newbie-question-on-an-i-o-loop/98580/2 "2006-07-16T12:15:39Z")

</div>

On 7/16/06, Jay D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

> \<\<\< 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  
# various possibilities  
|file|  
i = 0  
file.each\_byte {  
|c|  
i += 1  
puts “%d:%X” % [i, c]  
}  
end  
puts “at eof ;)”

Cheers  
Robert

–

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
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:** ![bloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e8c25b/32.png) [@bloc](https://www.ruby-forum.com/u/bloc)\
**Post date:** [July 16, 2006, 3:27pm UTC](https://www.ruby-forum.com/t/newbie-question-on-an-i-o-loop/98580/3 "2006-07-16T15:27:04Z")

</div>

Jay D. wrote:

> ```
> 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:** ![bloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e8c25b/32.png) [@bloc](https://www.ruby-forum.com/u/bloc)\
**Post date:** [July 17, 2006, 8:31am UTC](https://www.ruby-forum.com/t/newbie-question-on-an-i-o-loop/98580/4 "2006-07-17T08:31:15Z")

</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:

> > > > > > “J” == Jay D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> J\> and the next three (unread) bytes in the file are: 1A C0 B5  
> ^^^  
> 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:** ![bloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e8c25b/32.png) [@bloc](https://www.ruby-forum.com/u/bloc)\
**Post date:** [July 16, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/newbie-question-on-an-i-o-loop/98580/5 "2006-07-16T15:49:14Z")

</div>

> > > > > “J” == Jay D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

J\> and the next three (unread) bytes in the file are: 1A C0 B5  
^^^  
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
