# Reading binary values from a file

**URL:** <https://discourse.julialang.org/t/reading-binary-values-from-a-file/84936>\
**Category:** New to Julia\
**Tags:** binaryio\
**Created:** [July 28, 2022, 8:25pm UTC](https://discourse.julialang.org/t/reading-binary-values-from-a-file/84936 "2022-07-28T20:25:43Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [August 1, 2022, 10:18pm UTC](https://discourse.julialang.org/t/reading-binary-values-from-a-file/84936/4 "2022-08-01T22:18:27Z")

</div>

Hi Peter, why do we need `stream = ...`, in:

`stream = open(filename, "r") do stream ...` ?

Also, shouldn’t it be: `4*index - 4`, in:

`readvalue(stream, 4*index - 3)` ?

See a working example here:

```julia
function readvalue(stream, position)
    seek(stream, position)
    return read(stream, Int32)
end

function readvalues(filename::AbstractString, indices)
    open(filename, "r") do stream
        return [readvalue(stream, 4*index-4) for index in indices]
    end
end

# Writing test file:
x = Int32.(1:1000)
open("array_Int32.bin", "w") do io
    write(io, x, Int32[])
end

# Reading test file:
y = readvalues("array_Int32.bin", 1:10:1000)

```

Thanks.

---

_[View the full topic](https://discourse.julialang.org/t/reading-binary-values-from-a-file/84936)._
