Read multiple variables from binary file at once

Hi all, if a binary file contains, e.g. many intergers, how can I elegantly read them at once, Given the total number is known?
The most naive way to read them:

    open(filename) do f
        a = read(f, Int32)
        b = read(f, Int32)
        c = read(f, Int32)
        # and so on
        g = read(f, Int32)
        # do something here
    end

A simpler way which needs array allocation

    tmp = zeros(Int32, 7)
    open(filename) do f
        read!(f, tmp)
    end
    a, b, c, d, e, f, g = tmp
    # do something here

I hope I can do it like this:

    open(filename) do f
        a, b, c, d, e, f, g = read(f, Int32, 7)
        # do something here
    end
a, b, c, d, e, f, g = reinterpret(Int32, read(f, 7*sizeof(Int32))

but this would allocate an array on the right hand side

1 Like

A slightly more obvious alternative, but the f variable clash needs to be resolved in this case:

a, b, c, d, e, f, g = (read(f, Int32) for _ in 1:7)
2 Likes

Or this slightly more elegant version: read!(f, Vector{Int32}(undef, 7)).

3 Likes

@GunnarFarneback, sorry to ask but your code is not working as expected in Julia 1.7.0-beta4: all values returned are the same. What is happening here?

file = "seven_Int32.dat"
a,b,c,d,e,f,g = rand(Int32,7)
open(file,"w") do io
    write(io, a,b,c,d,e,f,g)
end

a,b,c,d,e,f,g = reinterpret(Int32, read(file, 7*sizeof(Int32)))   # OK
a,b,c,d,e,f,g = (read(file, Int32) for _ in 1:7)                  # Not OK
a,b,c,d,e,f,g = read!(file, Vector{Int32}(undef, 7))              # OK

Looks like you are repeatedly opening the file seven times to read the first Int32. Try to give the read an open stream and not a filename.

1 Like

@GunnarFarneback, thanks and sorry for the double-misunderstanding (thought it was a one-liner and that it would be possible to continue reading file from the last value). Yes, it does work with an open stream.