Newbie - Help with using puts to binary file

P

Peter Bailey

I am writing a templating system to replace tags and values in template
files. I am quite new to Ruby and am not sure I am going about things
correctly. My prog runs great on text files, rtf, etc., but I am running
into problems outputting to binary formats (e.g. word - sigh). My program
just reads the whole file into a string, works on that, and then I
output to another file using puts. This causes grief for word or open
office etc when it tries to open the newly created file.

I tried a simple test just reading in the file and writing it out, and I
see that the puts adds 1 byte to the eof, a LF (working on OS X). I got
the impression from reading docs that the puts would not mess with my
ouput, but that is obviously not the case, as my simple test has shown.

So, can anyone point me to a better way to write out the binary data so
that it is not altered (except by me on purpose). I looked at the data
input into a string, and it is correct. It is the output that is
breaking things.

Any help/pointers to explanations of how puts really works etc. or
better alternatives much appreciated.

Peter
 
A

Austin Ziegler

I am writing a templating system to replace tags and values in template
files. I am quite new to Ruby and am not sure I am going about things
correctly. My prog runs great on text files, rtf, etc., but I am running
into problems outputting to binary formats (e.g. word - sigh). My program
just reads the whole file into a string, works on that, and then I
output to another file using puts. This causes grief for word or open
office etc when it tries to open the newly created file.
=20
I tried a simple test just reading in the file and writing it out, and I
see that the puts adds 1 byte to the eof, a LF (working on OS X). I got
the impression from reading docs that the puts would not mess with my
ouput, but that is obviously not the case, as my simple test has shown.
=20
So, can anyone point me to a better way to write out the binary data so
that it is not altered (except by me on purpose). I looked at the data
input into a string, and it is correct. It is the output that is
breaking things.
=20
Any help/pointers to explanations of how puts really works etc. or
better alternatives much appreciated.

#puts will add an LF if it isn't present at the end of the output.
#print will not and #write will not. When I'm writing binary data, I
tend to use IO#write.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
A

Austin Ziegler

#puts will add an LF if it isn't present at the end of the output.
#print will not and #write will not. When I'm writing binary data, I
tend to use IO#write.

By the way, it won't affect Unix or OS X, but please remember to open
your output files as:

file =3D File.open(output, "wb")

This will make sure that binary files are properly handled in Windows.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top