# How to read a static array of a composite type

**URL:** <https://discourse.julialang.org/t/how-to-read-a-static-array-of-a-composite-type/115436>\
**Category:** Data\
**Tags:** question, struct, staticarrays, read\
**Created:** [June 10, 2024, 3:54pm UTC](https://discourse.julialang.org/t/how-to-read-a-static-array-of-a-composite-type/115436 "2024-06-10T15:54:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![s153283](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s153283/32/23290_2.png) [@s153283](https://discourse.julialang.org/u/s153283)\
**Post date:** [June 10, 2024, 3:54pm UTC](https://discourse.julialang.org/t/how-to-read-a-static-array-of-a-composite-type/115436/1 "2024-06-10T15:54:55Z")

</div>

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.

```julia
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?**
