为什么我的 IO.write 在输出末尾插入一个“%”符号?

Why does my IO.write insert a "%" sign at the end of output?

我用这一行从 temp.dat 中读取,其中包含“100”

fahrenheit = IO.read("temp.dat").to_i * 9 / 5 + 32

现在,把这个结果写入另一个文件;

方法一

f = File.new("temp.out", "w")
f.puts fahrenheit

猫temp.out

212

方法二

IO.write("temp.out", fahrenheit)

猫temp.out

212%

Why does my IO.write insert a “%” sign at the end of output?

没有。这是文件的二进制内容。 % 字符是您的 shell 的命令提示符,它因文件中缺少 EOL 而混淆。 POSIX 兼容的文本文件应始终以行尾字符结束行。