Binary-safe file IO

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

Carl Youngblood wrote:

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

Carl Youngblood wrote:

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?

 File.open("oldbinfile", "rb")
 File.open("newbinfile", "wb")

– Lyle

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

Hal Fulton wrote:

···

Carl Youngblood wrote:

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

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?

···

On Thu, 18 Dec 2003, Carl Youngblood wrote:

Thanks,

Carl

Yes, it should be OK on all of those platforms.

Chad

Hal Fulton wrote:

> Carl Youngblood wrote:

>

>> 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

>

>

>

“Carl Youngblood” carl@youngbloods.org schrieb im Newsbeitrag
news:Ap7Eb.573803$HS4.4315108@attbi_s01…

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
···

Thanks,
Carl

Hal Fulton wrote:

Carl Youngblood wrote:

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