来自 io:format 的 badarg 异常
badarg exception from io:format
我想写一个函数,可以接受一系列用 \n
分隔的数字
并将它们打印在列表中。但是,我无法在 badarg
错误方面取得任何进展。我该如何处理这段代码?这个想法是将数字通过管道传递给这个程序,但是当我传递多个数字时,我得到这个错误:
exception error: bad argument
in function io:format/3
called as io:format(<0.62.0>,"~w~n",[3,2,1])
in call from erl_eval:local_func/6 (erl_eval.erl, line 564)
in call from escript:interpret/4 (escript.erl, line 788)
in call from escript:start/1 (escript.erl, line 277)
in call from init:start_em/1
in call from init:do_boot/3
这是我的代码:
-module(prog).
-export([read_stdin/1]).
-export([main/0]).
read_stdin(Acc) ->
case io:fread(standard_io, '', "~d") of
eof -> Acc;
{ok, Line} -> read_stdin(Line ++ Acc)
end.
main() ->
Data = read_stdin([]),
io:format("~w~n", Data).
io:format
的第二个参数是 列表 值。即使您只使用一个控制序列使用一个值(在本例中为 ~w
),您也需要将值包装在一个列表中:
io:format("~w~n", [Data]).
我想写一个函数,可以接受一系列用 \n
分隔的数字
并将它们打印在列表中。但是,我无法在 badarg
错误方面取得任何进展。我该如何处理这段代码?这个想法是将数字通过管道传递给这个程序,但是当我传递多个数字时,我得到这个错误:
exception error: bad argument
in function io:format/3
called as io:format(<0.62.0>,"~w~n",[3,2,1])
in call from erl_eval:local_func/6 (erl_eval.erl, line 564)
in call from escript:interpret/4 (escript.erl, line 788)
in call from escript:start/1 (escript.erl, line 277)
in call from init:start_em/1
in call from init:do_boot/3
这是我的代码:
-module(prog).
-export([read_stdin/1]).
-export([main/0]).
read_stdin(Acc) ->
case io:fread(standard_io, '', "~d") of
eof -> Acc;
{ok, Line} -> read_stdin(Line ++ Acc)
end.
main() ->
Data = read_stdin([]),
io:format("~w~n", Data).
io:format
的第二个参数是 列表 值。即使您只使用一个控制序列使用一个值(在本例中为 ~w
),您也需要将值包装在一个列表中:
io:format("~w~n", [Data]).