我無法從文本文件中讀取IP並將其正確寫入其他文本文件。它示出了寫入IP的文件,如: 「ÿþ19 2 1 6 8 1 1 0 4。」無法將IP寫入文本文件而沒有格式問題
#Read the first line for the IP
def get_server_ip
File.open("d:\\ip_addr.txt") do |line|
a = line.readline()
b = a.to_s
end
end
#append the ip to file2
def append_ip
FileUtils.cp('file1.txt', 'file2.txt')
file_names = ['file2.txt']
file_names.each do |file_name|
text = File.read(file_name)
b = get_server_ip
new_contents = text.gsub('ip_here', b)
File.open(file_name, "w") {|file| file.puts new_contents }
end
end
我試圖.strip和.delete(」「),沒有運氣。任何人都可以看到問題嗎?
謝謝
該文件似乎是在Windows上使用記事本生成的。它的編碼很可能是'UTF-16LE'(可打印字符之間的空格是空字符)。 – axiac