julia

How to read a file into a vector?


The following code, which can be run in the Julia REPL, shows how to write the contents of a vector to disk.

v = rand(Int64, 1000000)
ofile = open("example_vector.bin", "w")
write(ofile, v)
close(ofile)

The reverse operation is presumably possible as well, however I cannot figure out the correct syntax for this.

ifile = open("example_vector.bin", "r")

v2 = Vector{Int64}(undef, 1000000)
read(ifile, v) # this is not valid, perhaps unsurprisingly

# I tried a few other things which I thought might work, but none did
read(ifile, Vector{Int64}, 1000000)
read(ifile, Vector{Int64, 1000000})
read(ifile, ::Type{Vector{Int64}}, 1000000)

I have already tried the following things:

What I want to avoid is having to make 1 million function calls to read to read each element of the vector individually, which is likely to result in poor performance.


Solution

  • Whenever you are looking for a function that works in-place or mutates an argument, you are looking for functions that end with !. In this case, you are looking for read!, which does exactly what you want:

    https://docs.julialang.org/en/v1/base/io-network/#Base.read!