Park Heesob
10/25/2007 3:04:00 PM
Hi,
----- Original Message -----
From: "Vianney Lecroart" <acemtp@gmail.com>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Thursday, October 25, 2007 11:36 PM
Subject: read write integer in binary into a file
> Hello,
>
> I have some big files with lot of "unsigned int" (4 bytes) numbers and I
> want to read and write on these files.
>
> Currently, I found this to write:
>
> myfile << [mynum].pack("i")
>
> and to read:
>
> mynum = myfile.read(4).unpack("i").first
>
> I wonder if there's not something faster/simpler to do that without the
> need to convert the number into an array into a string to finally
> serialize it.
>
> Thank you.
How about Marshal?
myfile << Marshal.dump(mynum)
and
mynum = Marshal.load(myfile.read)
Regards,
Park Heesob