# 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:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Schneeschaufel](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@Schneeschaufel](https://discourse.julialang.org/u/Schneeschaufel)
#### Post date: [July 28, 2022, 8:25pm UTC](https://discourse.julialang.org/t/reading-binary-values-from-a-file/84936/1 "2022-07-28T20:25:43Z")

</div>

I saw the following code ([http://www.samuelbosch.com/2014/07/benchmarking-reading-binary-values-from.html](http://www.samuelbosch.com/2014/07/benchmarking-reading-binary-values-from.html)) and I was intrigued how close the structure resembles Scheme or Lisp code (I wrote something similar in Bigloo 20 years ago):

```julia
 function readvalue(stream, position)
     seek(stream, position)
     return read(stream, Int32)
 end
 
 function readvalues(filename::String, indices)
     stream = open(filename, "r")
     try
         return Int32[readvalue(stream, index*4) for index in indices]
     finally
         close(stream)
     end
 end

```

The Julia code performed worst in the test (compared to Python, Go language, F#, Ocaml, with the exception of R). **I am not concerned with the Julia performance but wonder what needs to be changed to make the above code Julia idiomatic?**

By the way: I was also intrigued by the F# version:

```julia
open System
open System.IO
 
 let readValue (reader:BinaryReader) cellIndex = 
     reader.BaseStream.Seek(int64 (cellIndex*4), SeekOrigin.Begin) ignore
     match reader.ReadInt32() with
     | Int32.MinValue -> None
     | v -> Some(v)
         
 let readValues indices fileName = 
     use reader = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
     let values = Array.map (readValue reader) indices
     values

```

---

<div class="post-metadata">

### Author: ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)
#### Post date: [July 29, 2022, 2:51am UTC](https://discourse.julialang.org/t/reading-binary-values-from-a-file/84936/3 "2022-07-29T02:51:12Z")

</div>

It is more idiomatic to use a `do` block with the `open` function:

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

```

Also note that `indices` in Julia is by default 1-based, so you need to account for this in calling the `seek` function.

Edit: See correction of this code in the [follow-on post](https://discourse.julialang.org/t/reading-binary-values-from-a-file/84936/4) below.

---

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

---

<div class="post-metadata">

### Author: ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)
#### Post date: [August 1, 2022, 10:35pm UTC](https://discourse.julialang.org/t/reading-binary-values-from-a-file/84936/5 "2022-08-01T22:35:09Z")

</div>

You’re right on both points. The `stream = ` was an oversight left in from the OP’s code, but the `4*index-3` is a serious mistake. I wasn’t careful enough checking the semantics of `seek`, where I assumed that it was also 1-based! Thanks for the catch.
