Home All Groups Group Topic Archive Search About

Optimizing Binary File Input

Author
22 May 2005 9:57 AM
wxforecaster
I'm reading data from a file into a Byte Array. Please tell me there's a
faster way then as follows (reading the file one lowly byte at a time)...

Thanks for your time,
Evan


Public Function read_data(byVal FileName as String, byRef bytedata() as
Byte)

Dim File_Len as Long
Dim Filenumber as Integer

File_Len = FileLen(Filename)
ReDim bytedata(File_Len)

Filenumber = Freefile
Open FileName For Binary Input As #Filenumber  ' Open up the file

Do                                    ' Loop until there is no more data
    X = X + 1
    Get #Filenumber, X, bytedata(X)
Loop Until (EOF(Filenumber))

Close #Filenumber

End Function

Author
22 May 2005 10:13 AM
Mike D Sutton
> I'm reading data from a file into a Byte Array. Please tell me there's a
> faster way then as follows (reading the file one lowly byte at a time)...

Yup, just read it in one chunk:

'***
Filenumber = FreeFile()
Open FileName For Binary Access Read Lock Write As #Filenumber
    ReDim bytedata(0 To LOF(Filenumber) - 1) As Byte
    Get #Filenumber, , bytedata()
Close #Filenumber
'***

Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
22 May 2005 5:59 PM
Ulrich Korndoerfer
Hi,

Mike D Sutton schrieb:
>> I'm reading data from a file into a Byte Array. Please tell me there's a
>> faster way then as follows (reading the file one lowly byte at a time)...
>
> Yup, just read it in one chunk:
>
> '***
> Filenumber = FreeFile()
> Open FileName For Binary Access Read Lock Write As #Filenumber
>     ReDim bytedata(0 To LOF(Filenumber) - 1) As Byte
>     Get #Filenumber, , bytedata()
> Close #Filenumber
> '***

Or, if these byte data happens to be a sequence of singles:

Dim arrSingles() As Single
....
   ReDim arrSingles(0 To (LOF(Filenumber) / 4 - 1))
   Get Filenumber, , arrSingles
....

--
Ulrich Korndoerfer

VB tips, helpers, solutions -> http://www.proSource.de/Downloads/

-----------------------------------------------------------------------
Plea for a bright future for VB classic.
Sign the petition to Microsoft: -> http://classicvb.org/petition/
-----------------------------------------------------------------------