[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Getting binary data

Joe Van Dyk

4/2/2005 12:07:00 AM

Hi,

I've got a file that has some floats and ints at the beginning of the
file. The floats were added to the file with code like:

fprintf(file_ptr, "%f %f\n", some_float_1, some_float_2);
fprintf(file_ptr, "%f %f\n", some_float_3, some_float_4);
fprintf(file_ptr, "%d %d\n", some_int_1, some_int_2);
fprintf(file_ptr, "%d %d\n", some_int_3, some_int_4);

But I'm having the hardest time using unpack() to get the data. Any pointers?

Joe


1 Answer

Timothy Byrd

4/2/2005 12:30:00 AM

0

That doesn't sound like binary data to me. For example, this C
program:

#include "stdio.h"
int main() {
float some_float_1 = 42e23, some_float_2 = 23.42;
printf("%f %f\n", some_float_1, some_float_2);
return 0;
}

Produces:

4199999942871246500000000.000000 23.420000

If that's the case, something like this might answer:

filename = "data.txt"
IO.foreach(filename) { |line|
a = line.split
x = a[0].to_f
y = a[1].to_f

puts "x = #{x}, y = #{y}"
}

-- Timothy