[erlang-questions] read_file -- binary data object
Dan Sommers
2QdxY4RzWzUUiLuE@REDACTED
Thu Dec 20 02:22:53 CET 2018
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
More information about the erlang-questions
mailing list