I am trying to read arrays of composite types from some binary files using static arrays. I get into some issues reading the data because the sizeof
the composite type is not equal to the sizeof
the fields summed. Here is an small example.
using StaticArrays
struct LongCdsTime
day::UInt16
millisecond::UInt32
microsecond::UInt16
end
Base.read(io::IO, T::Type{LongCdsTime}) = T((read(io, t) for t in fieldtypes(T))...)
# test array
T = SMatrix{20,30,LongCdsTime}
# test data
test_file, fp = mktemp()
write(fp, rand(UInt8, 8*length(T)))
seekstart(fp)
a1 = read(fp, T) ## EOFError: read end of file
seekstart(fp)
a2 = T([ read(fp,eltype(T)) for i = 1:length(T)]) ## works fine
sizeof(LongCdsTime) ## 12 bytes
sum(sizeof.(fieldtypes(LongCdsTime))) ## 8 bytes
Question, What is the best way to read SMatrix{20,30,LongCdsTime}
when each element is stored as 8 bytes in a file?