[erlang-questions] Reading fixed structures from file
Gleb Peregud
gleber.p@REDACTED
Sat Mar 1 19:54:30 CET 2008
Hello.
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) ->
list:reverse(List);
scan_file(N, Binary, List) ->
<<
Key:4/little-integer-unit:8,
Adr:8/little-integer-unit:8,
Type:1/little-integer-unit:8,
Rest/binary
>> = 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
http://gleber.pl/
"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