# Read multiple variables from binary file at once

**URL:** <https://discourse.julialang.org/t/read-multiple-variables-from-binary-file-at-once/69409>\
**Category:** General Usage\
**Tags:** binaryio\
**Created:** [October 8, 2021, 2:33am UTC](https://discourse.julialang.org/t/read-multiple-variables-from-binary-file-at-once/69409 "2021-10-08T02:33:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsjie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsjie/32/22477_2.png) [@jsjie](https://discourse.julialang.org/u/jsjie)\
**Post date:** [October 8, 2021, 2:33am UTC](https://discourse.julialang.org/t/read-multiple-variables-from-binary-file-at-once/69409/1 "2021-10-08T02:33:00Z")

</div>

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:

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

```julia
    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:

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

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 8, 2021, 2:34am UTC](https://discourse.julialang.org/t/read-multiple-variables-from-binary-file-at-once/69409/2 "2021-10-08T02:34:59Z")

</div>

> [@jsjie](#):
>
> `a, b, c, d, e, f, g = read(f, Int32, 7)`

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

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 8, 2021, 7:07am UTC](https://discourse.julialang.org/t/read-multiple-variables-from-binary-file-at-once/69409/3 "2021-10-08T07:07:40Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [October 8, 2021, 4:30pm UTC](https://discourse.julialang.org/t/read-multiple-variables-from-binary-file-at-once/69409/4 "2021-10-08T16:30:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 8, 2021, 5:19pm UTC](https://discourse.julialang.org/t/read-multiple-variables-from-binary-file-at-once/69409/5 "2021-10-08T17:19:19Z")

</div>

@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?

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 8, 2021, 8:40pm UTC](https://discourse.julialang.org/t/read-multiple-variables-from-binary-file-at-once/69409/6 "2021-10-08T20:40:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 8, 2021, 8:51pm UTC](https://discourse.julialang.org/t/read-multiple-variables-from-binary-file-at-once/69409/7 "2021-10-08T20:51:36Z")

</div>

@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.
