[erlang-questions] read_file -- binary data object

Donald Steven t6sn7gt@REDACTED
Thu Dec 20 04:30:10 CET 2018


Solved, thanks everyone.  I was confused by erlang:binary_to_list and 
binary:bin_to_list.

On 12/19/2018 8.22 PM, Dan Sommers wrote:
> On 12/19/18 6:04 PM, Donald Steven wrote:
>
> > Taking your foo1,txt example, when I read it back
> > (file:read_file("foo1.txt"), how do I access the individual characters
> > of "Something I'll write to disk."  That is, if
> > file:read_file("foo1.txt") yields {ok,<<"Something I'll write to
> > disk.">>}, how do I read the "S", then the "o", etc.?
>
> What is your ultimate intent with all of those individual characters?
>
> (1) If you want to create a new list from them, then convert the binary
> to a list and use a list comprehension:
>
>     [operate_on_one_octet(Octet) || Octet <- binary:bin_to_list(Binary)]
>
> or perhaps lists:filter, lists:foldl, lists:map, etc.
>
> (2) If you want to run some side-effect-producing function on each one,
> then use lists:foreach:
>
>     lists:foreach(fun print_one_octet(Octet) ->
>                       io:format("--> ~p <--~n", [Octet])
>                   end,
>                   binary:bin_to_list([Binary]))
>
> Dan
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list