0
votes

I'm trying to read all the bytes of a file using Julia into an array. So far I have:

s = open(file_path,"r")

I'm not sure how to tell how big the file is. I'm also not sure that I need to. Perhaps I can just pass an empty array into readbytes!

2

2 Answers

5
votes

The simplest way do do it is to use read function. You can either pass an open stream to it like data = read(s) if s was opened with the code you have provided above. Alternatively you can simply write data = read(file_path). In this way you do not have to close the stream yourself.

You can find out the details by reading help of read by executing ?read in Julia REPL.

To get the size of the file in bytes you can use filesize(file_path) function.

0
votes

After a bit of testing this seems to work ...

s = open(file_path,"r")
data = UInt8[]
readbytes!(s,data,Inf)
close(s)