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))
function read_bin(filename, dims::N) where N<:Integer
stream = open(filename, "r");
data = read(stream, Float32, dims );
close(stream);
return data
end
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!..
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
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.
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
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.)