# Char conversion when using puts to write file lint by line

**URL:** <https://www.ruby-forum.com/t/char-conversion-when-using-puts-to-write-file-lint-by-line/166394>\
**Category:** Ruby\
**Created:** [April 24, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/char-conversion-when-using-puts-to-write-file-lint-by-line/166394 "2009-04-24T19:05:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabutz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8797f3/32.png) [@gabutz](https://www.ruby-forum.com/u/gabutz)\
**Post date:** [April 24, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/char-conversion-when-using-puts-to-write-file-lint-by-line/166394/1 "2009-04-24T19:05:41Z")

</div>

I’m trying to create a script to generate several win32 registry entries  
in a .reg file, using a template. The idea is that I have a registry  
entry in the template file with tokens to be replaced by values I  
determine.  
I’m getting a weir behavior when trying to do the following:

def createFromTemplate (src, template,filename)  
raise "Template file not found "+template if !File.file?(template)  
f = File.open(template)  
nf = File.new(filename,‘w+’)  
f.each{|line|  
nf.write line  
}  
end

I would expect this code to copy all the lines from the template file to  
the result file. Instead I’m getting the following:

Template file:  
Windows Registry Editor Version 5.00

[HKEY\_USERS\S-1-5-21-216280869-1618010840-3877511435-1006\Software\SimonTatham\PuTTY\Sessions]

[HKEY\_USERS\S-1-5-21-216280869-1618010840-3877511435-1006\Software\SimonTatham\PuTTY\Sessions\PORTAL001%20CMPT%201%20-%20DB%20Tunnels]  
“Present”=dword:00000001  
“HostName”=“172.18.142.94”  
…

Result file:  
Windows Registry Editor Version 5.00  
à¨à´€à´€  
[HKEY\_USERS\S-1-5-21-216280869-1618010840-3877511435-1006\Software\SimonTatham\PuTTY\Sessions]  
à¨à´€à´€  
[HKEY\_USERS\S-1-5-21-216280869-1618010840-3877511435-1006\Software\SimonTatham\PuTTY\Sessions\PORTAL001%20CMPT%201%20-%20DB%20Tunnels]  
à¨âˆ€å€€çˆ€æ”€çŒ€æ”€æ¸€ç€âˆ€ã´€æ€çœ€æ¼€çˆ€æ€ã¨€ã€€ã€€ã€€ã€€ã€€ã€€ã€€ã„€à´€à´€  
“HostName”=“172.18.142.94”  
…

Notice that the fn.puts call is adding unrecognized characters where in  
the template file I would find CRLF.  
If I do a File.copy, the file gets copyied without any char conversion.  
Can someone help me determine what might be happening? I’m clueless.

I’m running Ruby 1.9 on Windows XP.

Regards
