Detecting End of File

A

amiga1200

I am importing a file using the following code but I am having difficulty
determining the end of the file, Currently I just create a very large array &
hope it fits.

ReDim a(0 To 100000)
inputfile = "C:\Temp\Remittance.dat"
Open inputfile For Binary Access Read As #2
Get #2, , a
Close #2
For Cnt = 0 To UBound(a()) '100000
Str = Str & Chr(a(Cnt))
Next

Amiga1200
 
D

Douglas J. Steele

The FileLen function will return the size of the file in bytes. Use that to
determine what the upper limit of your array needs to be.
 
J

John Nurick

There's no need to loop through the array to assemble the string one
character at a time: you can "Get" straight from the file into a
presized string variable:

Dim strContents As String
Dim lngFN As Long

Const FName = "C:\TEMP\60kAsTruncated.txt"

lngFN = FreeFile()
Open FName For Binary Access Read As #lngFN
strContents = Space(LOF(lngFN)) 'allocate string
Get #lngFN, , strContents
Close #lngFN

Simpler still, use Input() instead of Get, which means you don't have to
pre-size the string:

strContents = Input(LOF(lngFN), #lngFN)
 
Top