My first post here, I’m newbie in ruby, maybe somebody could help me
with this.
I have a script that read/parse a binary file. The scripts works fine if
I run it in Ubuntu, but I’m trying
to run the code in IRB on Windows7 with Ruby version “2.0.0p247
(2013-06-27) [i386-mingw32]” and I receive
the following errors.
##########################################################################
C:\Scripts>ruby script.rb binaryfile
script.rb:18:in gets': encoding mismatch: CP850 IO with UTF-8 RS (ArgumentError) from script.rb:18:ingets’
from script.rb:18:in `’
##########################################################################
The script is like below (line 18 contains while gets):
FIle.open returns a file object, which you are not saving a reference to
As far as I know gets will default to standard in. does the app wait
till
you hit a key?
Thank you for your answers. I’ll try your suggestions.
I try to use “while gets” because the binary is divided by blocks, so
each time that appears the beginning of each block executes the code
inside “while gets”.
The issue is the binary files is 2GB in size and I don’t know why the
code works in linux under ruby 2.0 and doesnt work in windows with ruby
2.0.
On Sat, Sep 14, 2013 at 10:20 AM, Tamara T. [email protected] wrote:
(ArgumentError)
Difficult to know what is happening here.
Wondering if you should specify:
File.open(file, ‘rb’, :encoding => Encoding::UTF_8 ) do |f|
while (f.read(1024,buffer))
# process the buffer
end
end
You need to declare buffer:
irb(main):028:0> File.open(‘x’,‘rb’){|io| while io.read(1024, buffer);
puts buffer.bytesize; end}
NameError: undefined local variable or method buffer' for main:Object from (irb):28:inblock in irb_binding’
from (irb):28:in open' from (irb):28 from /usr/bin/irb:12:in’
Also, there seems to be no point in declaring an encoding when reading
binary.
File.open(file, ‘rb’) do |f|
buffer = ‘’
while f.read(1024, buffer)
# process the buffer
end
end
while f.read(1024, buffer)
line = $_.unpack(‘H*’)[0] # What should go instead of this line?
# process the buffer
end
end
############################################################
while gets
line = $_.unpack(‘H*’)[0]
next unless line =~ /Regexp/ #Some code using “line” content
end
##########################################################
Should be something like:
while line = gets(\xff\x45.force_encoding(“BINARY”))