read_file -- binary data object

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

read_file -- binary data object

Donald Steven
The manual for the function read_file/1 says: Returns {ok, Binary}, where Binary is a binary data object that contains the contents of Filename, or {error, Reason} if an error occurs.

What is the type of this "binary data object" (a list?).  (I'll want to access individual elements.)

Thanks.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: read_file -- binary data object

zxq9-2
On 2018年12月19日水曜日 18時40分05秒 JST Donald Steven wrote:
> The manual for the function read_file/1 says: Returns {ok, Binary}, where Binary is a binary data object that contains the contents of Filename, or {error, Reason} if an error occurs.
>
> What is the type of this "binary data object" (a list?).  (I'll want to access individual elements.)

It is an Erlang binary.


1> Foo1 = "Something I'll write to disk.".
"Something I'll write to disk."
2> Foo2 = <<"Something else I'll write to disk.">>.
<<"Something else I'll write to disk.">>
3> Foo3 = <<1,2,3,4>>.
<<1,2,3,4>>
4> file:write_file("foo1.txt", Foo1).
ok
5> file:write_file("foo2.txt", Foo2).
ok
6> file:write_file("foo3", Foo3).    
ok
7> file:read_file("foo1.txt").
{ok,<<"Something I'll write to disk.">>}
8> file:read_file("foo2.txt").
{ok,<<"Something else I'll write to disk.">>}
9> file:read_file("foo3").    
{ok,<<1,2,3,4>>}


If the above seems perplexing then check out the docs on Erlang binaries.
They are awesome, especially when dealing with binary file formats or network data.

http://erlang.org/doc/reference_manual/expressions.html#bit_syntax
http://erlang.org/doc/programming_examples/bit_syntax.html
http://erlang.org/doc/man/binary.html

Also, keep in mind that "object" is a heavily overloaded term in computing.
The docs mean "returns a self-contained thingy that can be labeled and carries
a type (binary) native to the runtime".

-Craig
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: read_file -- binary data object

Donald Steven
Thanks Craig.  I don't think I expressed my question well.

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

Don

On 12/19/2018 6.48 PM, [hidden email] wrote:

> On 2018年12月19日水曜日 18時40分05秒 JST Donald Steven wrote:
>> The manual for the function read_file/1 says: Returns {ok, Binary}, where Binary is a binary data object that contains the contents of Filename, or {error, Reason} if an error occurs.
>>
>> What is the type of this "binary data object" (a list?).  (I'll want to access individual elements.)
> It is an Erlang binary.
>
>
> 1> Foo1 = "Something I'll write to disk.".
> "Something I'll write to disk."
> 2> Foo2 = <<"Something else I'll write to disk.">>.
> <<"Something else I'll write to disk.">>
> 3> Foo3 = <<1,2,3,4>>.
> <<1,2,3,4>>
> 4> file:write_file("foo1.txt", Foo1).
> ok
> 5> file:write_file("foo2.txt", Foo2).
> ok
> 6> file:write_file("foo3", Foo3).
> ok
> 7> file:read_file("foo1.txt").
> {ok,<<"Something I'll write to disk.">>}
> 8> file:read_file("foo2.txt").
> {ok,<<"Something else I'll write to disk.">>}
> 9> file:read_file("foo3").
> {ok,<<1,2,3,4>>}
>
>
> If the above seems perplexing then check out the docs on Erlang binaries.
> They are awesome, especially when dealing with binary file formats or network data.
>
> http://erlang.org/doc/reference_manual/expressions.html#bit_syntax
> http://erlang.org/doc/programming_examples/bit_syntax.html
> http://erlang.org/doc/man/binary.html
>
> Also, keep in mind that "object" is a heavily overloaded term in computing.
> The docs mean "returns a self-contained thingy that can be labeled and carries
> a type (binary) native to the runtime".
>
> -Craig
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
by
Reply | Threaded
Open this post in threaded view
|

Re: read_file -- binary data object

by
This can be achieved by pattern matching.

Like
<<A:1,B:1,C>> = Bin.
A B C stores the specified data with type integer(character)

Yao

> 在 2018年12月20日,08:04,Donald Steven <[hidden email]> 写道:
>
> Thanks Craig.  I don't think I expressed my question well.
>
> 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.?
>
> Don
>
>> On 12/19/2018 6.48 PM, [hidden email] wrote:
>>> On 2018年12月19日水曜日 18時40分05秒 JST Donald Steven wrote:
>>> The manual for the function read_file/1 says: Returns {ok, Binary}, where Binary is a binary data object that contains the contents of Filename, or {error, Reason} if an error occurs.
>>>
>>> What is the type of this "binary data object" (a list?).  (I'll want to access individual elements.)
>> It is an Erlang binary.
>>
>>
>> 1> Foo1 = "Something I'll write to disk.".
>> "Something I'll write to disk."
>> 2> Foo2 = <<"Something else I'll write to disk.">>.
>> <<"Something else I'll write to disk.">>
>> 3> Foo3 = <<1,2,3,4>>.
>> <<1,2,3,4>>
>> 4> file:write_file("foo1.txt", Foo1).
>> ok
>> 5> file:write_file("foo2.txt", Foo2).
>> ok
>> 6> file:write_file("foo3", Foo3).
>> ok
>> 7> file:read_file("foo1.txt").
>> {ok,<<"Something I'll write to disk.">>}
>> 8> file:read_file("foo2.txt").
>> {ok,<<"Something else I'll write to disk.">>}
>> 9> file:read_file("foo3").
>> {ok,<<1,2,3,4>>}
>>
>>
>> If the above seems perplexing then check out the docs on Erlang binaries.
>> They are awesome, especially when dealing with binary file formats or network data.
>>
>> http://erlang.org/doc/reference_manual/expressions.html#bit_syntax
>> http://erlang.org/doc/programming_examples/bit_syntax.html
>> http://erlang.org/doc/man/binary.html
>>
>> Also, keep in mind that "object" is a heavily overloaded term in computing.
>> The docs mean "returns a self-contained thingy that can be labeled and carries
>> a type (binary) native to the runtime".
>>
>> -Craig
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: read_file -- binary data object

Donald Steven
When I try that A is bound to all the data in Bin.  I want A to be bound
only to the first byte.

On 12/19/2018 7.59 PM, by wrote:
> <<A:1,B:1,C>> = Bin.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: read_file -- binary data object

Dan Sommers
In reply to this post by Donald Steven
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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: read_file -- binary data object

Donald Steven
This doesn't work:

    case file:read_file(Fname) of
        {ok, Binary}   ->  InputBuffer = binary:bin_to_list([Binary]),
                            io:format("Character 4 is: ~p", [element(4, InputBuffer)]),

It prints all of the data, not just the fourth character.

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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: read_file -- binary data object

Donald Steven
In reply to this post by Dan Sommers
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
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: read_file -- binary data object

zxq9-2
In reply to this post by Donald Steven
On 2018年12月19日水曜日 19時04分17秒 JST you wrote:
> Thanks Craig.  I don't think I expressed my question well.
>
> 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.?

Gotcha

Once I have a binary I can loop over it the same way I would a list.
Continuing with our previous example of "foo1.txt"...



1> PrintCodepoints = fun
1>   P(<<Char, Rest/binary>>) ->
1>     ok = io:format("Codepoint: ~w~n", [Char]),
1>     P(Rest);
1>   P(<<>>) ->
1>     io:format("Done!~n")        
1>  end.
#Fun<erl_eval.30.99386804>
2> {ok, Bin} = file:read_file("foo1.txt").
{ok,<<"Something I'll write to disk.">>}
3> PrintCodepoints(Bin).
Codepoint: 83
Codepoint: 111
Codepoint: 109
Codepoint: 101
Codepoint: 116
Codepoint: 104
Codepoint: 105
Codepoint: 110
Codepoint: 103
Codepoint: 32
Codepoint: 73
Codepoint: 39
Codepoint: 108
Codepoint: 108
Codepoint: 32
Codepoint: 119
Codepoint: 114
Codepoint: 105
Codepoint: 116
Codepoint: 101
Codepoint: 32
Codepoint: 116
Codepoint: 111
Codepoint: 32
Codepoint: 100
Codepoint: 105
Codepoint: 115
Codepoint: 107
Codepoint: 46
Done!
ok


Hm. That came out a bit longer vertically than I intended, but you get the idea.

Obviously you would want to define that as a normal function, not a named lambda, but the shell is really convenient for just showing you how it would go.


codepoints(<<Char, Rest/binary>>) ->
    ok = io:format("Codepoint: ~w~n", [Char]),
    codepoints(Rest);
codepoints(<<>>) ->
    io:format("Done!~n").


Put anything in there you might want to imagine.


-Craig
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: read_file -- binary data object

Joe Armstrong-2
In reply to this post by zxq9-2
A binary is just an area of memory containing raw bytes.

L = binary_to_list(B) converts the binary B into a list of integers L where
each integer is in the range 0..255

list_to_binary(L) is the inverse.

A list of small integers of length N takes 16*N bytes of storage (on a
64 bit word size erlang)
A binary of size N takes N+C bytes of storage where C is a small constant

Random access on lists is bad - random access on binaries is far better.

There are primitives to access the individual elements of lists and binaries.

There are also some fancy bit-level things (the binary syntax) so you
can extract ranges of bits from binaries in a convenient manner.

Cheers

/Joe


On Thu, Dec 20, 2018 at 12:48 AM <[hidden email]> wrote:

>
> On 2018年12月19日水曜日 18時40分05秒 JST Donald Steven wrote:
> > The manual for the function read_file/1 says: Returns {ok, Binary}, where Binary is a binary data object that contains the contents of Filename, or {error, Reason} if an error occurs.
> >
> > What is the type of this "binary data object" (a list?).  (I'll want to access individual elements.)
>
> It is an Erlang binary.
>
>
> 1> Foo1 = "Something I'll write to disk.".
> "Something I'll write to disk."
> 2> Foo2 = <<"Something else I'll write to disk.">>.
> <<"Something else I'll write to disk.">>
> 3> Foo3 = <<1,2,3,4>>.
> <<1,2,3,4>>
> 4> file:write_file("foo1.txt", Foo1).
> ok
> 5> file:write_file("foo2.txt", Foo2).
> ok
> 6> file:write_file("foo3", Foo3).
> ok
> 7> file:read_file("foo1.txt").
> {ok,<<"Something I'll write to disk.">>}
> 8> file:read_file("foo2.txt").
> {ok,<<"Something else I'll write to disk.">>}
> 9> file:read_file("foo3").
> {ok,<<1,2,3,4>>}
>
>
> If the above seems perplexing then check out the docs on Erlang binaries.
> They are awesome, especially when dealing with binary file formats or network data.
>
> http://erlang.org/doc/reference_manual/expressions.html#bit_syntax
> http://erlang.org/doc/programming_examples/bit_syntax.html
> http://erlang.org/doc/man/binary.html
>
> Also, keep in mind that "object" is a heavily overloaded term in computing.
> The docs mean "returns a self-contained thingy that can be labeled and carries
> a type (binary) native to the runtime".
>
> -Craig
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: read_file -- binary data object

Donald Steven
Thanks Joe, much appreciated.

On 12/20/2018 4.49 AM, Joe Armstrong wrote:

> A binary is just an area of memory containing raw bytes.
>
> L = binary_to_list(B) converts the binary B into a list of integers L where
> each integer is in the range 0..255
>
> list_to_binary(L) is the inverse.
>
> A list of small integers of length N takes 16*N bytes of storage (on a
> 64 bit word size erlang)
> A binary of size N takes N+C bytes of storage where C is a small constant
>
> Random access on lists is bad - random access on binaries is far better.
>
> There are primitives to access the individual elements of lists and binaries.
>
> There are also some fancy bit-level things (the binary syntax) so you
> can extract ranges of bits from binaries in a convenient manner.
>
> Cheers
>
> /Joe
>
>
> On Thu, Dec 20, 2018 at 12:48 AM <[hidden email]> wrote:
>> On 2018年12月19日水曜日 18時40分05秒 JST Donald Steven wrote:
>>> The manual for the function read_file/1 says: Returns {ok, Binary}, where Binary is a binary data object that contains the contents of Filename, or {error, Reason} if an error occurs.
>>>
>>> What is the type of this "binary data object" (a list?).  (I'll want to access individual elements.)
>> It is an Erlang binary.
>>
>>
>> 1> Foo1 = "Something I'll write to disk.".
>> "Something I'll write to disk."
>> 2> Foo2 = <<"Something else I'll write to disk.">>.
>> <<"Something else I'll write to disk.">>
>> 3> Foo3 = <<1,2,3,4>>.
>> <<1,2,3,4>>
>> 4> file:write_file("foo1.txt", Foo1).
>> ok
>> 5> file:write_file("foo2.txt", Foo2).
>> ok
>> 6> file:write_file("foo3", Foo3).
>> ok
>> 7> file:read_file("foo1.txt").
>> {ok,<<"Something I'll write to disk.">>}
>> 8> file:read_file("foo2.txt").
>> {ok,<<"Something else I'll write to disk.">>}
>> 9> file:read_file("foo3").
>> {ok,<<1,2,3,4>>}
>>
>>
>> If the above seems perplexing then check out the docs on Erlang binaries.
>> They are awesome, especially when dealing with binary file formats or network data.
>>
>> http://erlang.org/doc/reference_manual/expressions.html#bit_syntax
>> http://erlang.org/doc/programming_examples/bit_syntax.html
>> http://erlang.org/doc/man/binary.html
>>
>> Also, keep in mind that "object" is a heavily overloaded term in computing.
>> The docs mean "returns a self-contained thingy that can be labeled and carries
>> a type (binary) native to the runtime".
>>
>> -Craig
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions