Reading binary file into a Vector of custom struct

@contradict and @Jordan_Cluts Thank you for your help!

This is the current way I have approached this problem. I’m sure it can be cleaned up some more, but for the time being this seems to work:

struct BinObj
    head1::UInt32
    head2::Float32
    ⋮
    data::Matrix{Float32} # Has size (40, 200)
    foot::UInt32
end # Has total size BINOBJSIZE

function _readIntoBinObj(iostream)
    head1 = read(iostream, UInt32)
    head2 = read(iostream, Float32)
    ⋮
    data = read!(iostream, Matrix{Float32}(undef, 40, 200))
    foot = read(iostream, UInt32)
    return BinObj(head1, head2, ..., data, foot)
end

function getBinObjVector(filename)
    n = filesize(filename) ÷ BINOBJSIZE
    io = open(filename)
    return [_readIntoBinObj(io) for i in 1:n]
end
1 Like