[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie question on an I/O loop

Jay Daniels

7/16/2006 8:26:00 AM

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

--
Posted via http://www.ruby-....

3 Answers

Steven R.

7/16/2006 1:20:00 PM

0

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

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.

ts

7/16/2006 1:48:00 PM

0

>>>>> "J" == Jay Daniels <jkdaniels@rock.com> 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



Jay Daniels

7/17/2006 6:31:00 AM

0

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 Daniels <jkdaniels@rock.com> 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


--
Posted via http://www.ruby-....