Binary-safe file IO

C

Carl Youngblood

Hi, I'm trying to embed a binary file inside a ruby script but I'm
having trouble. To test it out, I wrote a simple script that will take
a binary file and encode it to base64 and then decode it again and write
out the result. Here it is:

require 'base64'
bindata = File.open('oldbinfile').read
File.open('newbinfile', 'w') do |f|
f.write decode64(encode64(bindata))
end

When I run this, newbinfile is not identical to oldbinfile anymore.
Anyone know what I'm doing wrong? I'm assuming that the File IO
functions are not binary safe, but what should I use instead?

Thanks,
Carl Youngblood
 
H

Hal Fulton

Carl said:
Hi, I'm trying to embed a binary file inside a ruby script but I'm
having trouble. To test it out, I wrote a simple script that will take
a binary file and encode it to base64 and then decode it again and write
out the result. Here it is:

require 'base64'
bindata = File.open('oldbinfile').read
File.open('newbinfile', 'w') do |f|
f.write decode64(encode64(bindata))
end

When I run this, newbinfile is not identical to oldbinfile anymore.
Anyone know what I'm doing wrong? I'm assuming that the File IO
functions are not binary safe, but what should I use instead?

Are you on Windows? If so, 'wb' as a mode might help (binary mode).

Hal
 
C

Carl Youngblood

Is this only applicable to Windows then? I happened to be on Windows at
the time, but I'm developing this application cross-platform and testing
it on Windows, linux and OS X. Will this code still work on other
platforms?

Thanks,
Carl
 
R

Robert Klemme

Carl Youngblood said:
Is this only applicable to Windows then? I happened to be on Windows at
the time, but I'm developing this application cross-platform and testing
it on Windows, linux and OS X. Will this code still work on other
platforms?

Should be done on all platforms to maintain portability and for
documentation IMHO.

robert
 

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,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top