From: Robert Dober Date: 2006-07-16T19:14:48+09:00 Subject: Re: Newbie question on an I/O loop ------=_Part_40173_11621626.1153044885934 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 7/16/06, Jay Daniels 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 > 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] =3D=3D nil > puts( "\n" + $help ) > else > i =3D 0 > tfm =3D File.open( ARGV[0] ) > tfm.each_byte{ |c| i =3D 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 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 =3D 0 file.each_byte { |c| i +=3D 1 puts "%d:%X" % [ i, c ] } end puts "at eof ;)" Cheers Robert -- > Posted via http://www.ruby-forum.com/. > > --=20 Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_40173_11621626.1153044885934--