user2138149
user2138149

Reputation: 17276

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.

Upvotes: 0

Views: 64

Answers (2)

DNF
DNF

Reputation: 12664

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!

Upvotes: 2

CaptainTrunky
CaptainTrunky

Reputation: 1707

read has an overload that likely would help you:

read(s::IOStream, nb::Integer; all=true)

It reads at most n bytes from an input stream and stores them as Vector{UInt8}, as a raw binary data. Afterwards, it would be your job to coerce this binary data to expected Vector{Int64}. This solution likely to be more efficient compared to reading bytes one-by-one, though, it requires O(N) linear scan for coercion and O(N) additional memory.

Upvotes: 1

Related Questions