get the bytes of a file to an array

37 views Asked by At

I use the following 9 lines to put the bytes of a file NameF in vector T() in FreeBasic 0.4.6 FBide.

Is there a better or shorter way?

DIM L as ulong, NameF as string
Input "Enter Name of the File", NameF
Open NameF For Binary As #1
L= LOF(1)
Close #1
DIM T(1 to L) As Ubyte
Open NameF For Binary Access Read As #1
Get #1, , T()
Close #1

The 9 lines work OK. I am looking for improvement if feasible.

1

There are 1 answers

0
Sep Roland On

2 lines off is an improvement, thanks to everyone

Then 3 lines off is better (no need for the separate L variable, as well as the redundant Close and Open commands):

DIM NameF as string
Input "Enter Name of the File", NameF
Open NameF For Binary Access Read As #1
DIM T(1 to LOF(1)) As Ubyte
Get #1, , T()
Close #1