# Reading binary file in julia 1.0

**URL:** <https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951>\
**Category:** New to Julia\
**Tags:** binaryio\
**Created:** [August 23, 2018, 3:50pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951 "2018-08-23T15:50:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![feboccaccio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feboccaccio/32/9240_2.png) [@feboccaccio](https://discourse.julialang.org/u/feboccaccio)\
**Post date:** [August 23, 2018, 3:50pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/1 "2018-08-23T15:50:03Z")

</div>

The following function used to work fine in julia 0.6 for reading a file.  
I am having a hard time trying to use it in julia 1.0 (I get the message: no method matching read(::IOStream, ::Type{Float32}, ::Int64))

```julia
function read_bin(filename, dims::N) where N<:Integer
    stream = open(filename, "r");
    data = read(stream, Float32, dims );
    close(stream);
    return data
end

```

any suggestions? thanks.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 23, 2018, 3:55pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/2 "2018-08-23T15:55:31Z")

</div>

See: [PSA: use Julia 0.7 if you are upgrading](https://discourse.julialang.org/t/psa-use-julia-0-7-if-you-are-upgrading/13321) which says:

> The 0.7 release exists exactly for this reason: your old code will work on 0.7 and give you a specific warning about how to fix your code.

Also, please quote your code: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

<div class="post-metadata">

**Author:** ![feboccaccio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feboccaccio/32/9240_2.png) [@feboccaccio](https://discourse.julialang.org/u/feboccaccio)\
**Post date:** [August 23, 2018, 4:29pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/3 "2018-08-23T16:29:11Z")

</div>

Thanks a lot for replying!

Using julia 0.7 I got the following warning:  
Warning: `read(s::IO, t::Type, d1::Int, dims::Int...)` is deprecated, use `read!(s, Array{t}(undef, d1, dims...))` instead.

However, I am still doing something wrong. This time concerning types I am passing to read!..

Anyhow, thanks again!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 23, 2018, 6:03pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/4 "2018-08-23T18:03:08Z")

</div>

If you can give more information about the new error you’re seeing, perhaps we can help with that as well.

---

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [August 29, 2019, 4:51pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/5 "2019-08-29T16:51:39Z")

</div>

I have the same problem. When I use the read\_bin function, the read data is missing. What best way to read a binary file?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 29, 2019, 4:56pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/6 "2019-08-29T16:56:12Z")

</div>

You can simply do

```julia
open(read, filename)

```

`open` creates an `IO` object. The first argument to `open` is a function which is evaluated on the `IO` object, in this case `read` which simply collects the contents of the stream as a `Vector{UInt8}`.

Alternatively, you can read using memory mapping with

```julia
using Mmap
Mmap.mmap(filename)

```

This will return a memory-mapped `Vector{UInt8}`.

---

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [August 29, 2019, 5:05pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/7 "2019-08-29T17:05:09Z")

</div>

It could be that I’m doing it wrong. But it’s not working. Can I send the file to experiment?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 29, 2019, 5:12pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/8 "2019-08-29T17:12:01Z")

</div>

> [@Yoshua](#):
>
> But it’s not working

Define “not working”, could you show me the stack trace? It works for me on any file I try it on.

---

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [August 29, 2019, 5:17pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/9 "2019-08-29T17:17:21Z")

</div>

The returned data is the type 0x00 for all array.

---

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [August 29, 2019, 5:20pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/10 "2019-08-29T17:20:34Z")

</div>

I’m read the same file in R using readBin function, but the returned data is diferent.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 29, 2019, 5:23pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/11 "2019-08-29T17:23:12Z")

</div>

That doesn’t make sense to me. Are you sure it’s all zeros? Can you take the `sum` of the data and confirm that it’s zero? What is the file, what are you expecting to see?

The only thing I can think of is that R is expecting some sort of specialized format and is showing you data starting at some offset. The Julia functions will give you the entire file from the first bit to the last.

---

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [August 29, 2019, 5:30pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/12 "2019-08-29T17:30:07Z")

</div>

yes, in R i define byte and dimension. The datas are complex from the file .mlc .

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 29, 2019, 5:45pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/13 "2019-08-29T17:45:17Z")

</div>

> [@Yoshua](#):
>
> yes, in R i define byte and dimension.

That makes sense then, Julia is just giving you the complete buffer as a flat `Vector`. If you know the offset you want to start at, you can do, for example

```julia
Mmap.mmap(filename)[offset:end]

```

You can reshape the array (from `Vector` to any higher rank array) using `reshape`, you can see documentation on this function by doing `?reshape` in the REPL.

(A word of warning, though `mmap` is desirable in many cases, you can wind up writing to the file if you are not careful. If you are very worried about this happening, use `open(read, filename)` instead.)

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [August 29, 2019, 10:38pm UTC](https://discourse.julialang.org/t/reading-binary-file-in-julia-1-0/13951/14 "2019-08-29T22:38:35Z")

</div>

> [@ExpandingMan](#):
>
> (A word of warning, though `mmap` is desirable in many cases, you can wind up writing to the file if you are not careful. If you are very worried about this happening, use `open(read, filename)` instead.)

`Mmap.mmap` docs say it supports read only opening, but I think the call looks like `Mmap.map(open(filename,"r"))` in that case.
