# write number as type T
function writenum(io, num, T)
write(io, T(num))
end
# read n numbers of type T
function readnum(io, T, n)
s = sizeof(T)
f = zeros(UInt8, s*n)
readbytes!(io, f, s*n)
return reinterpret(T, f)
end
# read single number of type T
function readnum(io, T)
s = sizeof(T)
f = zeros(UInt8, s)
readbytes!(io, f, s)
return reinterpret(T, f)[1]
end
# write string
function writestr(io, str)
write(io, codeunits(str))
end
# read string of length n
function readstr(io, n)
return String(read(io, n))
end
# Test
io = open("io.bin", "w")
snd = "abc"
nsnd = length(snd)
writenum(io, nsnd, UInt8)
writestr(io, snd)
close(io)
io = open("io.bin", "r")
nrcv = readnum(io, UInt8)
rcv = readstr(io, nrcv)
close(io)
(nrcv == nsnd) && (snd == rcv)
Thx for staying with me!
I consulted this doc, it does read/write a whole file, a line, using delimiters, but not the case I am looking for, a single string.
The whole is for a serializer and inter-process communication. There are of course already proven packages. I use it as a learning vehicle (getting around…) and hope to come up with something faster, using a small set of homogeneous object types.
Not sure what you mean by more elegant. Is the do syntax from the first help example more elegant for your use case?
open(f::Function, args...; kwargs....)
Apply the function f to the result of open(args...; kwargs...) and close the resulting file descriptor upon completion.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> open("myfile.txt", "w") do io
write(io, "Hello world!")
end;
julia> open(f->read(f, String), "myfile.txt")
"Hello world!"
julia> rm("myfile.txt")
Interestingly, with the do form I learned that it creates a scope, making it less usable to get the information “out”.
function round_trip(data)
open("io.bin", "w") do io
serialize(io, data)
end
io = open("io.bin", "r")
data2 = deserialize(io)
println("data2=$data2")
open("io.bin", "r") do io
data3 = deserialize(io)
println("data3=$data3")
end
println("data3=$data3")
end
data = ("Ab", [pi, 2.0])
round_trip(data)
data2=("Ab", [3.141592653589793, 2.0])
data3=("Ab", [3.141592653589793, 2.0])
ERROR: LoadError: UndefVarError: data3 not defined
For a novice like me, pure magic - thx a lot for these lines!
Can you recommend a book or other text to get familiar with such things?
The manual states
do
Create an anonymous function and pass it as the first argument to a function call. For example:
map(1:10) do x
2x
end
is equivalent to map(x->2x, 1:10).
OK for the do now.
The second and third form are due to (specific I guess) Julia syntax for open():
Personally, I am not familiar with any of the julia books out there. I suggest reading the julia manual though. In particular, read the “Manual” section of the documentation (potentially excluding sections that seems irrelevant at the moment – e.g. asynchronous programming, if that isn’t important to you). Then also read the “Documentation” section. Everything up through Iteration Utilities. Beyond that, you might also need to look through sections of Standard Library documentation, as necessary. Going through all of this will expose you to a lot of aspects of julia you aren’t yet familiar with, and help explain ones you’re not yet clear about.