Sam Weaver
Sam Weaver

Reputation: 1079

How do I read ALL of standard input in Erlang?

I've found several questions about how to read from STDIN in Erlang, but each of them seems focused on reading one piece of data, or one line at at time.

If I'm piping data to my Erlang program, how can I read all of the data that's been piped to STDIN and then do something with it once the input buffer is flushed? My use case is parsing JSON.

Upvotes: 2

Views: 174

Answers (1)

Sam Weaver
Sam Weaver

Reputation: 1079

This snippet seems to do the trick:

read_all_stdin() ->
    read_all_stdin([]).
read_all_stdin(Acc) ->
    case io:get_line("") of
        eof -> lists:reverse(Acc);
        Line -> read_all_stdin([Line|Acc])
    end.

Upvotes: 0

Related Questions