Getting binary data

J

Joe Van Dyk

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
 
T

Timothy Byrd

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
 

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,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top