Ruby, “incompatible character encodings: UTF-8 and CP852 (Encoding::CompatibilityError)”

后端 未结 3 1519
萌比男神i
萌比男神i 2021-01-22 06:37

Why

# encoding: utf-8
out=File.open(\"z\\\\test.txt\", \"a\") 
out.puts \"ç\"  
out.close
out=File.open(\"z\\\\test.txt\", \"r\")
puts out.read+\"ś\"
         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-22 07:03

    Your script works fine for me on my box.

    Is the error coming from your terminal application or from Ruby?

    My terminal app is set to use utf-8.

    You can probably avoid this problem by explicitly supplying an encoding when opening your files. See http://www.ruby-doc.org/core/classes/File.html#M000069 and follow the links to IO::new.

提交回复
热议问题