grant2088
grant2088

Reputation: 33

badarg exception from io:format

I want to write a function that can take a series of numbers separated with \n and print them in a list. However I cannot make any headway of the badarg error. How can I proceed with this code? The idea is to pipe the numbers to this program, but when I pass more than one number I get this error:

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 

Here is my code:

-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).

Upvotes: 1

Views: 1242

Answers (1)

legoscia
legoscia

Reputation: 41668

The second argument to io:format is a list of values. Even if you only use one control sequence using a value (~w in this case), you need to wrap the value in a list:

    io:format("~w~n", [Data]).

Upvotes: 6

Related Questions