【问题标题】:Why does my IO.write insert a "%" sign at the end of output?为什么我的 IO.write 在输出末尾插入一个“%”符号?
【发布时间】:2015-04-18 09:21:18
【问题描述】:

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

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

现在,将此结果写入另一个文件;

方法一

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

猫温度输出

212

方法二

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

猫温度输出

212%

【问题讨论】:

    标签: ruby file io


    【解决方案1】:

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

    它没有。这是文件的二进制内容。 % 字符是你的 shell 的命令提示符,它被文件中缺少 EOL 弄糊涂了。符合 POSIX 标准的文本文件应始终以换行符结束行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多