Hi all, if a binary file contains, e.g. many intergers, how can I elegantly read them at once, Given the total number is known?
The most naive way to read them:
open(filename) do f
a = read(f, Int32)
b = read(f, Int32)
c = read(f, Int32)
# and so on
g = read(f, Int32)
# do something here
end
A simpler way which needs array allocation
tmp = zeros(Int32, 7)
open(filename) do f
read!(f, tmp)
end
a, b, c, d, e, f, g = tmp
# do something here
I hope I can do it like this:
open(filename) do f
a, b, c, d, e, f, g = read(f, Int32, 7)
# do something here
end
@GunnarFarneback, sorry to ask but your code is not working as expected in Julia 1.7.0-beta4: all values returned are the same. What is happening here?
file = "seven_Int32.dat"
a,b,c,d,e,f,g = rand(Int32,7)
open(file,"w") do io
write(io, a,b,c,d,e,f,g)
end
a,b,c,d,e,f,g = reinterpret(Int32, read(file, 7*sizeof(Int32))) # OK
a,b,c,d,e,f,g = (read(file, Int32) for _ in 1:7) # Not OK
a,b,c,d,e,f,g = read!(file, Vector{Int32}(undef, 7)) # OK
@GunnarFarneback, thanks and sorry for the double-misunderstanding (thought it was a one-liner and that it would be possible to continue reading file from the last value). Yes, it does work with an open stream.