Erlang io:unicode字符的fread不能与-noshell一起使用
我正在尝试使用Erlang io:unicode字符的fread不能与-noshell一起使用,erlang,Erlang,我正在尝试使用io:fread读取unicode字符(超出拉丁1范围) 下面的代码在shell模式下运行良好: Eshell V5.10.4 (abort with ^G) 1> io:fread("Input some Unicode characters: ", "~ts"). Input some Unicode characters: 呵呵 {ok,[[21621,21621]]} 但是,当使用-noshell标志时,它会返回另一个列表: $ erl -noshell
io:fread
读取unicode字符(超出拉丁1范围)
下面的代码在shell模式下运行良好:
Eshell V5.10.4 (abort with ^G)
1> io:fread("Input some Unicode characters: ", "~ts").
Input some Unicode characters: 呵呵
{ok,[[21621,21621]]}
但是,当使用-noshell
标志时,它会返回另一个列表:
$ erl -noshell -eval "io:format(\"~p\", [io:fread(\"Input: \", \"~ts\")])."
Input: 呵呵
{ok,[[229,145,181,229,145,181]]}
有人知道它为什么会这样吗?来自erlang
当Erlang以-oldshell或-noshell启动时,用于
标准io默认设置为字节编码,而交互式
shell默认为环境变量所说的内容
使用io:setopts/2函数,您可以设置文件或文件的编码
其他I/O服务器
因此,io:setopts/2
应如下添加
$ erl -noshell -eval "io:setopts(standard_io, [{encoding, unicode}]), io:format(\"~p\", [io:fread(\"Input: \", \"~ts\")])."
Input: 呵呵
{ok,[[21621,21621]]}
谢谢,它很有效。现在输入是正确的,但在我将编码选项设置为unicode(或utf8)后,输出将变成混乱的字符。你对此有什么线索吗?我不确定是否有其他解决方案,但位语法似乎可以解决这个问题
erl-noshell-eval“io:setopts([{encoding,utf8}]),io:format(\“~ts~n\”,[])。”
我已经发现了问题。我使用的是Erlang R16,尽管它可以编译utf-8源文件,但它仍然将所有字符串文本视为拉丁1编码。必须将注释“%%--编码:utf-8--”添加到源文件中,然后才能使用utf-8字符串文字。在R17中,默认编码已更改为utf-8。