Binary-read fixed-length String

Thank you both for the solutions!


I’m finding that Julia’s binary reading isn’t as simple as Fortran’s. The datafile I’m reading was actually created by a Fortran program. To read it in Fortran, it’s just

integer:: iflag
character(4):: str
real(8):: arr(100,200)
open(newunit=uni, access="STREAM",  . . .)
read(uni) iflag, str, arr

while Julia uses at least three different methods:

iflag = read(io, Int32)
str = String(read(io, 4)) # four-byte string
arr = Array{Float32}(undef, (100,200))
read!(io,arr)

So, I was wondering if it’s possible to write a generic binary-read function, read_bin!(v...), which can read objects of simple types:

iflag::Int32 = 0
str::String = ""
arr = Array{Float32}(undef, (100,200))
read_bin!(io, iflag, (str, 4), arr)

or

iflag, str, arr = read_bin(io, Int32, (String, 4), (Array{Float32, 2}, (100, 200)) )