Reading binary files with a fixed struct

P

Psychotron

In C i've used to do a single fread passing a struct.
In ruby what is the best method?

I mean, without making subsequent reads, i've many fields of 2/4 bytes
each)

Thanks.
 
T

ts

P> In C i've used to do a single fread passing a struct.
P> In ruby what is the best method?

String#unpack ?

i.e. read xxx characters then use String#unpack
 
R

Ross Bamford

In C i've used to do a single fread passing a struct.
In ruby what is the best method?

I mean, without making subsequent reads, i've many fields of 2/4 bytes
each)

You probably want to look up String#unpack.
 
R

Robert Klemme

ts said:
P> In C i've used to do a single fread passing a struct.
P> In ruby what is the best method?

String#unpack ?

i.e. read xxx characters then use String#unpack

And don't forget to open the file in binary mode.

robert
 
P

Psychotron

Ross Bamford said:
You probably want to look up String#unpack.

Thanks, now i have an Array with all my fields, but if i want these
values in a meaningful Hash?

I can put an Array (less verbose than an Hash with null values) :

keys = %w{field1 field2 field2} #many fields with a better name

then i take my values :

values = f.read(64).unpack('VVVVvvvvvvvvVvvvvVVVVVV')

I can create an Hash from two arrays without iterating all elements?
 
R

Robert Klemme

Psychotron said:
b flag i think is useful only in windows enviroment.
In unix is ok, but i've don't tested in windows for now.

It's useful on Unix as well - if only for documentation purposes. This
also prevents that your script suddenly breaks if transferred to another
platform.

Cheers

robert
 
R

Ross Bamford

Thanks, now i have an Array with all my fields, but if i want these
values in a meaningful Hash?

I can put an Array (less verbose than an Hash with null values) :

keys = %w{field1 field2 field2} #many fields with a better name

then i take my values :

values = f.read(64).unpack('VVVVvvvvvvvvVvvvvVVVVVV')

I can create an Hash from two arrays without iterating all elements?

You could try something like:

keys = [:a, :b, :c]
values = [1, 2, 3]

h = Hash[*keys.zip(values).flatten]
# => {:b=>2, :a=>1, :c=>3}
 
J

Jeremy Henty

Thanks, now i have an Array with all my fields, but if i want these
values in a meaningful Hash?

Are you sure you want a Hash ? Perhaps a Struct class would work
better. It's very easy to turn arrays into structs. Here's an
example where I created a Struct class with some named members, then
turned an array into a struct, then recovered the members of the
original array by examining the members of the struct.

$ irb
irb(main):001:0> class Foo < Struct.new:)x,:y,:z) ; end
=> nil
irb(main):002:0> arr = [ :foo, "bar", 23, ]
=> [:foo, "bar", 23]
irb(main):003:0> struct = Foo.new(*arr)
=> #<struct Foo x=:foo, y="bar", z=23>
irb(main):004:0> struct.x
=> :foo
irb(main):005:0> struct.y
=> "bar"
irb(main):006:0> struct.z
=> 23
irb(main):007:0>

You can also access struct members in the same way as Arrays and
Hashes: struct[:x] is the same as struct.x .

Hope this helps,

Jeremy Henty
 
P

Psychotron

Jeremy Henty said:
Are you sure you want a Hash ? Perhaps a Struct class would work
better. It's very easy to turn arrays into structs. Here's an
example where I created a Struct class with some named members, then
turned an array into a struct, then recovered the members of the
original array by examining the members of the struct.

Not sure, but i agree with you. I'll try Struct in a second
implementation.

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top