How to read binary file with char 0x1A

(hsun) #1

Hi,

I tried to read a binary file with a char '0x1A' (EOF on window I guess). The method

File::each_byte stops at this char.

How can get it to read the whole file? Is there a binary mode for file operation?

Thanks

(Eric Hodel) #2

File.open name, 'rb' do |fp|
   # ...
end

PS: File::each_byte references a class method. You meant File#each_byte.

···

On 31 Aug 2005, at 21:24, hsun wrote:

Hi,

I tried to read a binary file with a char '0x1A' (EOF on window I guess). The method

File::each_byte stops at this char.
How can get it to read the whole file? Is there a binary mode for file operation?

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

(W. James) #3

hsun wrote:

Hi,

I tried to read a binary file with a char '0x1A' (EOF on window I guess).
The method

File::each_byte stops at this char.

How can get it to read the whole file? Is there a binary mode for file operation?

Thanks

  contents = open(filename, "rb") { |f| f.read }

(Ara.T.Howard) #4

open(path, 'rb') do |f|
   ...
end

hth.

-a

···

On Thu, 1 Sep 2005, hsun wrote:

Hi,

I tried to read a binary file with a char '0x1A' (EOF on window I guess). The method

File::each_byte stops at this char. How can get it to read the whole file? Is there a binary mode for file operation?

Thanks

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna

===============================================================================

(SHIGETOMI, Takuhiko) #5

hello.

> File::each_byte stops at this char.
>
> How can get it to read the whole file? Is there a binary mode for file operation?

  contents = open(filename, "rb") { |f| f.read }

see also: IO.binmode

q2hdp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618

(Brian Schröder) #6

That is one of the things that I did not understand for a long time,
because File#each_byte is valid ruby, a constant and a comment, and I
did not get that it is a way to say "the method of a file instance".

So I just wanted to pop in here and say:

    File#each means each is a method of a File instance

maybe that helps someone with the same confusion I had :wink:

regards,

Brian

···

On 01/09/05, Eric Hodel <drbrain@segment7.net> wrote:

On 31 Aug 2005, at 21:24, hsun wrote:

> Hi,
>
> I tried to read a binary file with a char '0x1A' (EOF on window I
> guess). The method
>
> File::each_byte stops at this char.
> How can get it to read the whole file? Is there a binary mode for
> file operation?

File.open name, 'rb' do |fp|
   # ...
end

PS: File::each_byte references a class method. You meant
File#each_byte.

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/