[erlang-questions] Reading fixed structures from file

Gleb Peregud gleber.p@REDACTED
Sat Mar 1 19:54:30 CET 2008


I have one simple question regarding reading a fixed structure from files.

Let say i have a binary file filled with sequence of fixed length
structures. Each structure is of the form:

struct {
 int32 key,
 int64 adr,
 char type

File was written at x86 platform, hence it is little-endian. I use R12B

Is the following way the best way to read whole file to the list:

scan_file(Binary) ->
    scan_file(0, Binary, []).
scan_file(N, <<>>, List) ->
scan_file(N, Binary, List) ->
     >> = Binary,
    Obj = {N, Type, Adr, Key},
    scan_file(N+1, Rest, [Obj | List]).

Code was not tested.

I have doubts if i understand notion of "size" and "unit" in binary
matching... Do i understand it correctly?

Gleb Peregud
"Only two things are infinite, the universe and human stupidity, and
I'm not sure about the former."
-- Albert Einstein

More information about the erlang-questions mailing list